javascript - NodeJS Express 无法接收 POST json

标签 javascript jquery json node.js

我正在尝试将 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/

相关文章:

javascript - ng-repeat 显示 json 对象 Angular JS

javascript - 如何检测当前触摸移动下方的元素

jquery - 使用一键切换淡入/淡出

javascript - Hello World 不能在带有 jointjs 的 html 中正常工作?

javascript - 转到基于 ul li 的选择框中的指定文本

android - 如何处理json字符串中的特殊字符

php - Android php 如果方向不起作用如何避免错误关闭应用程序

javascript - jquery 动画不透明度、淡入淡出和向上滑动页面加载效果的正确方法

Javascript 正则表达式匹配由非数字字符分隔的数字

c++ - Poco C++ 构建嵌套的 JSON 对象