【问题标题】:What are "res" and "req" parameters in Express functions?Express 函数中的“res”和“req”参数是什么?
【发布时间】:2011-06-09 10:30:51
【问题描述】:

在以下 Express 函数中:

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

reqres 是什么?它们代表什么,它们的含义是什么,它们的作用是什么?

谢谢!

【问题讨论】:

  • req == "request" // res == "response"
  • 我也想补充一下我的意见,我们应该更喜欢使用参数名称作为请求/响应而不是请求/响应,因为只有字符差异,这可能成为错误的原因,一次我们的代码库增加了。谢谢。

标签: node.js express


【解决方案1】:

请求和响应。

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

【讨论】:

  • 这没有帮助;控制台中的输出是 [object Object]。
  • 如果你想要json,你必须:console.log(JSON.Stringify(req.body);
【解决方案2】:

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

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

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

编辑:

假设你有这个方法:

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

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

  • 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")。

要响应该请求,您可以使用响应对象来构建响应。扩展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);
});

【讨论】:

  • 您可以使用 curl 查看完整的带有标头的响应
  • 您可能想查看:en.wikipedia.org/wiki/Hypertext_Transfer_Protocol。不要刻薄,这是我们所有为 Web 开发的人都需要了解的事情!
  • 是的,这很棒,应该在 express.js 网站的主页上。
  • expressnoob - 响应是一个对象,就像请求对象一样,但它包含与响应相关的字段和方法。通常使用响应的 send() 方法。 send() 接受一大堆不同类型的第一个参数,它成为 HTTP 响应正文,第二个参数是 HTTP 响应代码。
  • 如果有人正在寻找reqres 结构的详细信息,请参阅快递文档:req:expressjs.com/en/api.html#req,res:expressjs.com/en/api.html#res
【解决方案3】:

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

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

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

并且,如果您已将 express 配置为将其 bodyparser (app.use(express.bodyParser());) 也与 POST'ed formdata 一起使用。 (见How to retrieve POST query parameters?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    相关资源
    最近更新 更多