Javascript 在使用语法Api时无法获取/
我正在尝试使用API检查句子的语法。但是当我不知道它给了我错误时,请告诉我哪里错了Javascript 在使用语法Api时无法获取/,javascript,node.js,api,Javascript,Node.js,Api,我正在尝试使用API检查句子的语法。但是当我不知道它给了我错误时,请告诉我哪里错了 const express = require('express'); const https = require('https'); const app = express(); app.get('/', function(req, res){ res.sendFile(__dirname + "/index.html"); }); app.post(&quo
const express = require('express');
const https = require('https');
const app = express();
app.get('/', function(req, res){
res.sendFile(__dirname + "/index.html");
});
app.post("/", function(req, res){
const text = req.body.cityName;
const url = "https://www.stands4.com/services/v2/grammar.php?uid=8816&tokenid=u9K3iEcIcjmS84B6&text=" + text + "&format=json";
https.get(url,function(response){
response.on("data", function(data){
const grammar = JSON.parse(data);
const fault = grammar.matches.message;
console.log(fault);
res.write("<p>The problem in text :" + fault + "</p>");
res.send();
});
});
});
app.listen(port = 3000, function(){
console.log("Server is running on port 3000 ");
})
const express=require('express');
常量https=require('https');
常量app=express();
app.get('/',函数(req,res){
res.sendFile(uu dirname+“/index.html”);
});
应用程序post(“/”,功能(请求,回复){
const text=req.body.cityName;
常量url=”https://www.stands4.com/services/v2/grammar.php?uid=8816&tokenid=u9K3iEcIcjmS84B6&text=“+text+”&format=json”;
https.get(url、函数(响应){
响应。关于(“数据”,函数(数据){
const grammar=JSON.parse(数据);
const fault=grammar.matches.message;
控制台日志(故障);
res.write(“文本中的问题:“+fault+””;
res.send();
});
});
});
app.listen(端口=3000,函数(){
log(“服务器正在端口3000上运行”);
})
您是否试图通过浏览器访问localhost:3000?如果您正在使用app.post
将其更改为app.get
如果您试图使用自己的API,那么问题在于您的控制器是一个post控制器。这就是为什么它会抛出您无法获取的
您可以将app.post
更改为app.get
,或者,您可以尝试调用post方法而不是get。此错误通常发生在以无效端点为目标时,请确保使用正确的url和正确的方法。您的控制器是post控制器,因此会出现此错误。您可以进行post呼叫,也可以切换到控制器以获取
app.get("/", function(req, res) ...