gpt4 book ai didi

node.js - 在 Express 中获取 POST 和 GET 请求的表单数据

转载 作者:搜寻专家 更新时间:2023-10-31 23:38:33 25 4
gpt4 key购买 nike

我想在 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!
});

谢谢

最佳答案

您正在寻找 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

关于node.js - 在 Express 中获取 POST 和 GET 请求的表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26066583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com