node.js Express 函数中的“res”和“req”参数是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4696283/
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 13:49:07  来源:igfitidea点击:

What are "res" and "req" parameters in Express functions?

node.jsexpress

提问by expressnoob

In the following Express function:

在以下 Express 函数中:

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

What are reqand res? What do they stand for, what do they mean, and what do they do?

什么是reqres?它们代表什么,它们是什么意思,它们是做什么的?

Thanks!

谢谢!

回答by Dave Ward

reqis an object containing information about the HTTP request that raised the event. In response to req, you use resto send back the desired HTTP response.

req是一个对象,包含有关引发事件的 HTTP 请求的信息。响应req,您使用res发送回所需的 HTTP 响应。

Those parameters can be named anything. You could change that code to this if it's more clear:

这些参数可以任意命名。如果更清楚,您可以将该代码更改为:

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

Edit:

编辑:

Say you have this method:

假设你有这个方法:

app.get('/people.json', function(request, response) { });

The request will be an object with properties like these (just to name a few):

请求将是一个具有以下属性的对象(仅举几例):

  • request.url, which will be "/people.json"when this particular action is triggered
  • request.method, which will be "GET"in this case, hence the app.get()call.
  • An array of HTTP headers in request.headers, containing items like request.headers.accept, which you can use to determine what kind of browser made the request, what sort of responses it can handle, whether or not it's able to understand HTTP compression, etc.
  • An array of query string parameters if there were any, in request.query(e.g. /people.json?foo=barwould result in request.query.foocontaining the string "bar").
  • request.url,这将是"/people.json"在触发此特定操作时
  • request.method"GET"在这种情况下就是这样,因此app.get()调用。
  • 中的 HTTP 标头数组request.headers,包含诸如 之类的项目request.headers.accept,您可以使用它们来确定发出请求的浏览器类型、它可以处理的响应类型、是否能够理解 HTTP 压缩等。
  • 一个查询字符串参数数组,如果有的话,在request.query(例如/people.json?foo=bar会导致request.query.foo包含字符串"bar")。

To respond to that request, you use the response object to build your response. To expand on the people.jsonexample:

要响应该请求,您可以使用 response 对象来构建您的响应。扩展people.json示例:

app.get('/people.json', function(request, response) {
  // We want to set the content-type header so that the browser understands
  //  the content of the response.
  response.contentType('application/json');

  // Normally, the data is fetched from a database, but we can cheat:
  var people = [
    { name: 'Dave', location: 'Atlanta' },
    { name: 'Santa Claus', location: 'North Pole' },
    { name: 'Man in the Moon', location: 'The Moon' }
  ];

  // Since the request is for a JSON representation of the people, we
  //  should JSON serialize them. The built-in JSON.stringify() function
  //  does that.
  var peopleJSON = JSON.stringify(people);

  // Now, we can use the response object's send method to push that string
  //  of people JSON back to the browser in response to this request:
  response.send(peopleJSON);
});

回答by Myrne Stol

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)

我注意到 Dave Ward 的回答中有一个错误(也许是最近的变化?):查询字符串参数在request.query,而不是request.params。(见https://stackoverflow.com/a/6913287/166530

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

request.params默认情况下填充路由中任何“组件匹配”的值,即

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?)

并且,如果您已将 express 配置为将其 bodyparser ( app.use(express.bodyParser());) 也与 POST'ed formdata 一起使用。(请参阅如何检索 POST 查询参数?

回答by generalhenry

Request and response.

请求和响应。

To understand the req, try out console.log(req);.

要了解req,请尝试console.log(req);