Modify Node.js req object parameters Modify Node.js req object parameters express express

Modify Node.js req object parameters


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.

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

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.


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');


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.query object.

req.query.q = 'something';

The same goes for req.body and req.params.