2016-05-14 76 views
1
的NodeJS我

有下面的代码post请求,当我通过邮递员使POST请求我得到req.body为未定义请求体为空在

POST请求 http://localhost:1702/es

身体:

{ “IP_A”: “191.XXXX”, “PKTS”:34

}

和Content-Type:“application/json”我还使用了application/x-www-form-urlencoded,但得到了相同的结果。

我app.js是

var express = require('express'); 
var es=require('./routes/es'); 
var app = express(); 
app.post('/es',es.postesdata); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 

我的文件,其中我收到一个请求体为null

exports.postesdata=function(req,res){ 

    var body=req.body; 

    console.log(body);//Getting Undefined here 

} 

上午我在这里做得不对。

回答

5

express运行中间件为了尝试:

app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true })); 
app.post('/es',es.postesdata); 
+0

得到了我的错误,我有app.post第一前app.use这就造成了身体undefined.Thank你 –

+0

我有同样的问题。我将上面的代码移到了应用程序的下方,获得如下初始化: //创建快速应用程序 var app = express(); //解析content-type的请求 - application/x-www-form-urlencoded app.use(bodyParser.urlencoded({extended:true})); //解析content-type -application/json的请求 app.use(bodyParser.json()); – Mohsin