获取Express中POST和GET请求的表单数据

Bra*_*ram 3 forms post node.js express

我想在Express/Node.js中获取BOTH POST和GET请求的参数值.我知道明确获取POST或GET数据的方法,但我想要一些适合两者的方法.这可能在一行代码中吗?

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

谢谢

Jor*_*ias 5

你在找req.param(name, [defaultValue]).

来自Express API参考

Lookup is performed in the following order:

req.params
req.body
req.query
Run Code Online (Sandbox Code Playgroud)

POST是 req.body

GET是 req.query

express.all('/page', function(req, res) {
    var thing = req.param('thing');
});
Run Code Online (Sandbox Code Playgroud)

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