res.location() 方法在 Express.js 中
res.location() 方法用於將響應的位置 HTTP 標頭設定為指定的 path 引數。設定位置並不會結束程序,設定位置標頭後還是可以傳送一些響應。
語法
res.location( path )
示例 1
建立一個名為 "resLocation.js" 的檔案,並複製以下程式碼段。建立檔案後,使用命令 "node resLocation.js" 來執行此程式碼,如下面的示例所示 −
// res.location(path) Method Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
app.get('/api', function(req, res){
res.location('https://tutorialspoint.tw');
console.log(res.get('location')); //
https://tutorialspoint.tw
res.end();
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});使用 GET 請求訪問以下端點 − localhost:3000/api
輸出
C:\home
ode>> node resLocation.js Server listening on PORT 3000 https://tutorialspoint.tw
示例 2
我們來看另一個示例。
// res.location(path) Method Demo Example
// Importing the express module
var express = require('express');
// Initializing the express and port number
var app = express();
// Initializing the router from express
var router = express.Router();
var PORT = 3000;
// Defining an endpoint
app.get('/api', function(req, res){
res.location('/v1/users/profile');
console.log("Location Path is: ", res.get('location')); //
https://tutorialspoint.tw
res.send(res.get('location'));
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});使用 GET 請求訪問以下端點 − localhost:3000/api
輸出
C:\home
ode>> node resLocation.js Server listening on PORT 3000 Location Path is: /v1/users/profile
廣告欄
資料結構
聯網
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP