【问题标题】:Get form data for both POST and GET requests in Express在 Express 中获取 POST 和 GET 请求的表单数据
【发布时间】:2014-11-21 21:06:21
【问题描述】:

我想在 Express/Node.js 中获取 POST 和 GET 请求的参数值。我知道可以显式获取 POST 或 GET 数据的方法,但我想要对两者都适用的方法。这可以在一行代码中实现吗?

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

谢谢

【问题讨论】:

    标签: node.js forms post express


    【解决方案1】:

    您正在寻找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.body 和req.query。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-08
      • 2011-12-16
      • 1970-01-01
      • 2020-07-19
      • 2022-11-07
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多