当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Express.js res.location()用法及代码示例


res.location()函数用于将响应位置HTTP标头设置为指定的path参数。本质上是用来设置响应头的。它不会结束响应,使用完后,您可以编写响应正文。

用法:

res.location( path )

参数:

  • path:它引用在请求的Referer标头中指定的URL。

快递模块的安装:

  1. 您可以访问安装Express模块​​的链接。您可以使用此命令安装此软件包。
    npm install express
  2. 安装Express模块​​后,可以使用命令在命令提示符下检查Express版本。
    npm version express
  3. 之后,您可以仅创建一个文件夹并添加一个文件,例如index.js。要运行此文件,您需要运行以下命令。
    node index.js

文件名:index.js



var express = require('express'); 
var app = express(); 
var PORT = 3000; 
  
app.get('/', function(req, res){ 
    res.location('http://demo.com'); 
    console.log(res.get('location')); // http://demo.com 
}); 
  
app.listen(PORT, function(err){ 
    if (err) console.log(err); 
    console.log("Server listening on PORT", PORT); 
});

运行程序的步骤:

  1. 项目结构将如下所示:
  2. 确保使用以下命令安装了Express模块​​:
    npm install express
  3. 使用以下命令运行index.js文件:
    node index.js

    输出:

    Server listening on PORT 3000
    
  4. 现在打开浏览器并转到http://localhost:3000 /,现在检查控制台,您将看到以下输出:
    Server listening on PORT 3000
    http://demo.com
    

相关用法


注:本文由纯净天空筛选整理自gouravhammad大神的英文原创作品 Express.js | res.location() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。