修改 Node.js req 对象参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18801661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:36:53  来源:igfitidea点击:

Modify Node.js req object parameters

node.jsexpressjavascript

提问by Levi Roberts

So as usual, I tried to find this question on SO but still no luck.

所以像往常一样,我试图在 SO 上找到这个问题,但仍然没有运气。

I am aware that the answer is "Yes, you CAN modify the req object" but nothing is said about the req object parameters.

我知道答案是“是的,您可以修改 req 对象”,但没有提及 req 对象参数。

For example the following code will throw an error:

例如下面的代码会抛出一个错误:

app.get('/search', function(req, res) {
   req.param('q') = "something";
});

Error:

错误:

ReferenceError: Invalid left-hand side in assignment


I imagine this has something to do with the property not having a 'SET' method or something along those lines.


我想这与没有“SET”方法或类似方法的属性有关。

There are a few scenarios where this could come in handy.

在某些情况下,这可能会派上用场。

  1. A service that turns quick-links into full blown requests and proxies those out.
  2. Simply modifying the parameters before sending it off to another function that you have no desire to modify.
  1. 一种将快速链接转换为完整请求并将其代理出去的服务。
  2. 在将参数发送到您不想修改的另一个函数之前,只需修改参数即可。

Onto the question, Is there a way to modify the req object parameters?

关于这个问题,有没有办法修改 req 对象参数?

回答by Roy Reiss

Rather than:

而不是:

req.param('q') = "something";

You'll need to use:

你需要使用:

req.params.q = "something";

The first one is trying to set a value on the return value of the param function, not the parameter itself.

第一个是尝试在 param 函数的返回值上设置一个值,而不是参数本身。

It's worth noting the req.param()method retrieves values from req.body, req.paramsand req.queryall at once and in that order, but to set a value you need to specify which of those three it goes in:

值得注意的是,req.param()方法从req.body, 中检索值,req.params按此req.query顺序一次全部检索,但要设置一个值,您需要指定它进入这三个中的哪一个:

req.body.q = "something";
// now: req.param('q') === "something"
req.query.r = "something else";
// now: req.param('r') === "something else"

That said unless you're permanently modifying something submitted from the client it might be better to put it somewhere else so it doesn't get mistaken for input from the client by any third party modules you're using.

也就是说,除非您要永久修改从客户端提交的内容,否则最好将其放在其他地方,以免您正在使用的任何第三方模块将其误认为是来自客户端的输入。

回答by Aqib Mumtaz

Alternative approaches to set params in request (use any):

在请求中设置参数的替代方法(使用任何):

                    req.params.model = 'Model';
Or
                    req.params['model'] = 'Model';
Or
                    req.body.name = 'Name';
Or
                    req.body['name'] = 'Name';
Or
                    req.query.ids = ['id'];
Or
                    req.query['ids'] = ['id'];

Now get params as following:

现在获取参数如下:

        var model = req.param('model');
        var name = req.param('name');
        var ids = req.param('ids');

回答by Andreas Hultgren

One can also imagine that the left-hand side is not a variable or property, so it makes no sense to try to assign a value to it. If you want to change a query param you have to modify the property in the req.queryobject.

还可以想象左侧不是变量或属性,因此尝试为其赋值是没有意义的。如果要更改查询参数,则必须修改req.query对象中的属性。

req.query.q = 'something';

The same goes for req.bodyand req.params.

这同样适用于req.bodyreq.params