2014-09-26 99 views
2

我想在Express/Node.js中獲取BOTH POST和GET請求的參數值。我知道將顯式獲取POST或GET數據的方法,但我希望能爲兩者都起作用。這可能在一行代碼中嗎?在Express中獲取POST和GET請求的表單數據

express.all('/page', function(req, res) { 
    var thing = req.body.thing; // only works for POST requests, not GET! 
}); 

感謝

回答

4

您正在尋找req.param(name, [defaultValue])

Express API Reference

Lookup is performed in the following order: 

req.params 
req.body 
req.query 

POST是req.body

GET是req.query

express.all('/page', function(req, res) { 
    var thing = req.param('thing'); 
}); 

或者你可以直接使用req.bodyreq.query