res.append() 方法在 Express.js 中
res.append() 方法可以向 HTTP 響應頭欄位附加一個指定值。如果尚未建立,它將使用指定的值建立新的頭。value 字串既可以採用字串輸入,也可以採用陣列輸入。
語法
res.append( field, [value] )
示例 1
建立一個檔案並命名為 "resAppend.js",然後複製以下程式碼段。建立該檔案後,使用命令 "node resAppend.js" 來執行此程式碼,如下面示例所示 −
// res.append(field, [value]) 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){
// Appending the localhost URL as link
res.append('Link', ['',''])
console.log(res.get('Link')); // URL Array
});
app.listen(PORT, function(err){
if (err) console.log(err);
onsole.log("Server listening on PORT", PORT);
});使用 GET 請求訪問以下 Endpoint − https://:3000/api
輸出
C:\home
ode>> node resAppend.js Server listening on PORT 3000 [ '', '' ]
示例 2
讓我們來看一個示例。
// res.append(field, [value]) 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){
// Setting string value to header
res.append('Set-Cookie', 'welcome=tutorialsPoint; Path=/;HttpOnly')
console.log(res.get('Set-Cookie'));
});
app.listen(PORT, function(err){
if (err) console.log(err);
console.log("Server listening on PORT", PORT);
});使用 GET 請求訪問以下 Endpoint − https://:3000/api
輸出
C:\home
ode>> node resAppend.js Server listening on PORT 3000 welcome=tutorialsPoint; Path=/; HttpOnly
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP