我正在尝试将 JSON 发送到我的 NodeJS 路由。
curl -H "Content-Type: application/json" -d '{"name":"homer"}' http://localhost:3000/api
所以,在我的 server.js 中:
...
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json({extended:true}));
app.use(methodOverride('_method'));
...
然后,在我的 route :
router.post('/api', function (req, res){
console.log(req.body);
});
因此,输出显示未定义
我做错了什么吗?我正在使用 Express v4。
最佳答案
Express 4 中的中间件和路由按照添加到应用程序的顺序执行。因此,您需要确保您的路由在使用 bodyParser
中间件之后出现。
关于javascript - NodeJS Express 无法接收 POST json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25771782/