我正在尝试部署一个node.js Heroku应用程序,它监听请求并将编码的字符串记录到控制台。 注意:我仅限于使用JSON从我的程序导出数据,这就是我使用req.body
的原因。
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!')
})
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
非常感谢你的时间
答案 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!');
});