简单的Node.js疑难解答 – 从app.get()接收404

我正在尝试部署一个node.js Heroku应用程序,该应用程序侦听请求并简单地将编码的stringlogging到控制台。 注意:我仅限于从JSON中导出我的程序中的数据,这就是为什么我要使用req.body


JavaScript的

 const express = require('express') const app = express() app.get('/um', function (req, res) { console.log(req.body[message]); }) app.listen(process.env.PORT || 3000, function () { console.log('App listening!') }) 

LUA

 local httpService = game:GetService('HttpService') httpService:PostAsync("http://auto-attica.herokuapp.com/um", { "message" = "foo" }) 

而不是logging文本到控制台。 我在运行Lua的程序和Heroku控制台上都收到错误404。

 2017-10-30T00:10:04.826245+00:00 heroku[router]: at=info method=POST path="/um" host=auto-attica.heroku-f35fbe137630 fwd="95.148.67.104" dyno=web.1 connect=1ms service=5ms status=404 bytes=386 protocol=http 

并在Lua控制台上

 00:16:02.074 - HTTP 404 (HTTP/1.1 404 Not Found) 00:16:02.074 - Stack Begin 00:16:02.075 - Script 'ServerScriptService.myOwnVanilla', Line 8 00:16:02.075 - Stack End 

非常感谢您的宝贵时间

您正在快速路由中定义一个GET请求,但试图发送一个POST请求到它,这是您的Heroku日志显示给您的。

 2017-10-30T00:10:04.826245+00:00 heroku[router]: at=info method=POST path="/um" ^^^^ 

可以尝试设置POST处理程序:

 app.post('/um', function (req, res) { console.log(req.body[message]); }) 

从上面的代码,特别是这行代码req.body [message] ,我不知道你是如何得到那个消息的。 当您尝试访问该端点时,为什么不发送响应。 示例如下所示:

 const express = require('express') const app = express(); app.get('/um', function (req, res) { res.json({ message: 'Its working' }); }); app.listen(process.env.PORT, function () { console.log('App listening!'); });