2013-07-25 89 views
0

我使用req.body我得到了一個未定義或空{}當使用express2.js:express3 JS req.body未定義

exports.post = function (req: express3.Request, res: express3.Response) { 
    console.log(req.body);  
}); 

我有以下配置:

app.use(express.bodyParser()); 
app.use(app.router); 

app.post('/getuser', routes.getuserprofile.post); 

的請求正文使用XML,我檢查了正確的請求標題。

+0

爲什麼你在參數中有':express3.Request'事物? – moka

回答

1

我錯過了你有XML的部分。我想默認情況下不會解析req.body。

如果使用快速2.x的話,或許this solution通過@DavidKrisch是適當的(下面複製)

// This script requires Express 2.4.2 
// It echoes the xml body in the request to the response 
// 
// Run this script like so: 
// curl -v -X POST -H 'Content-Type: application/xml' -d '<hello>world</hello>' http://localhost:3000 
var express = require('express'), 
    app = express.createServer(); 

express.bodyParser.parse['application/xml'] = function(data) { 
    return data; 
}; 

app.configure(function() { 
    app.use(express.bodyParser()); 
}); 


app.post('/', function(req, res){ 
    res.contentType('application/xml'); 
    res.send(req.body, 200); 
}); 

app.listen(3000); 
0

我不相信express.bodyParser()支持XML。它只支持url編碼參數和JSON。

來自:http://expressjs.com/api.html#middleware

bodyParser()

請求體解析中間件支撐JSON,urlencoded進行,和 多部分請求。