什么是“res"?和“请求"Express 函数中的参数? [英] What are "res" and "req" parameters in Express functions?

查看:23
本文介绍了什么是“res"?和“请求"Express 函数中的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的Express函数中:

In the following Express function:

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

什么是reqres?它们代表什么,它们是什么意思,它们有什么作用?

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

谢谢!

推荐答案

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

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

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

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

假设你有这个方法:

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

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

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

  • request.url,当这个特定的动作被触发时会是 "/people.json"
  • request.method,在这种情况下将是 "GET",因此是 app.get() 调用.
  • request.headers 中的一组 HTTP 标头,包含诸如 request.headers.accept 之类的项目,您可以使用它们来确定发出请求的浏览器类型,它可以处理什么样的响应,它是否能够理解 HTTP 压缩等.
  • request.query 中的查询字符串参数数组(例如 /people.json?foo=bar 将导致 request.query.foo 包含字符串 "bar").
  • 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=bar would result in request.query.foo containing the string "bar").

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

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

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

这篇关于什么是“res"?和“请求"Express 函数中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆