栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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()
    称为通话。
  • 中的HTTP标头数组
    request.headers
    ,包含诸如之类的项目
    request.headers.accept
    ,您可以使用它们确定哪种类型的浏览器发出请求,可以处理哪种响应,是否能够理解HTTP压缩等。
  • 查询字符串参数(如果有)的数组
    request.query
    (例如,
    /people.json?foo=bar
    将导致
    request.query.foo
    包含string
    "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);});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/448425.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号