[英]How can I get the body of the postrequest in nodejs?
我開始關注node.js並嘗試構建發布請求,這是我的server.js文件:
module.exports = function(app) {
app.post('/api/postrequest', function(req, res) {
console.log('inside postrequest');
console.log(req.body); //this is empty????
});
// application -------------------------------------------------------------
app.get('*', function(req, res) {
res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});
};
我想從請求中過濾數據,因此請嘗試驗證req.body中的內容。 問題是它是空的:{}。 要測試,我正在使用Postman提交請求:
http://localhost:8080/api/postrequest
使用原始格式發布此json:
{"name":"Eddie"}
您將需要使用諸如body-parser之類的中間件來進行此操作。 執行npm install body-parser
,並在您的應用程序中要求
var bodyParser = require('body-parser');
然后像這樣使用它
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({'extended': 'true'}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.