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