简单的Node.js故障排除 - 从app.get()接收404

时间:2017-10-30 00:17:43

标签: javascript node.js heroku lua

我正在尝试部署一个node.js Heroku应用程序,它监听请求并将编码的字符串记录到控制台。 注意:我仅限于使用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" })

而不是将文本记录到控制台。我在运行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

非常感谢你的时间

2 个答案:

答案 0 :(得分:1)

您正在快速路由中定义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]);
})

答案 1 :(得分:0)

从您上面的代码中,特别是这行代码 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!');
});