What are “res” and “req” parameters in Express functions?

后端 未结 3 1715
悲哀的现实
悲哀的现实 2020-11-29 14:28

In the following Express function:

app.get(\'/user/:id\', function(req, res){
    res.send(\'user\' + req.params.id);
});

What are re

3条回答
  •  心在旅途
    2020-11-29 15:02

    I noticed one error in Dave Ward's answer (perhaps a recent change?): The query string paramaters are in request.query, not request.params. (See https://stackoverflow.com/a/6913287/166530 )

    request.params by default is filled with the value of any "component matches" in routes, i.e.

    app.get('/user/:id', function(request, response){
      response.send('user ' + request.params.id);
    });
    

    and, if you have configured express to use its bodyparser (app.use(express.bodyParser());) also with POST'ed formdata. (See How to retrieve POST query parameters? )

提交回复
热议问题