I'm getting a "Type Error: Cannot read property of undefined" in this line of code

I'm getting a "Type Error: Cannot read property of undefined" in this line of code

我正在尝试测试通过 faye 从客户端发送的消息。

curl -X POST -H "Content-Type:application/json" -d '{"message":"Hi there."}' http://localhost:8000/message

这是罪犯线。

TypeError: Cannot read property &#39;message&#39; of undefined<br>

更新:罪魁祸首可能真的在这里...

app.post('/message', function(req, res) {
  bayeux.getClient().publish('/channel', {text: req.body.message});
  res.send(200);
});

不幸的是,我收到了这个错误。出于某种原因,它将消息视为未定义 属性,我不确定为什么。

在此先感谢您的帮助。

您缺少必需的正文解析中间件,例如 body-parser.json() 中间件。

安装该模块并添加

var bodyParser = require('body-parser');
app.use(bodyParser.json());

在你的路线之前的某个地方。