response.addTrailers()(在v0.3.0中添加)方法是“ http”模块的内置应用程序编程接口,该接口向响应添加HTTP尾部标头(标头,但位于消息末尾)。仅在将分块编码用于响应时才发出预告片。如果不是(例如,如果请求是HTTP /1.0),则会将其静默丢弃。 HTTP要求发送Trailer标头以发出预告片,并在其值中包含标头字段的列表。
如果已使用response.setHeader()设置标头,则它们将与传递给response.writeHead()的任何标头合并,并且标头传递给response.writeHead()优先。
为了获得响应和正确的结果,我们需要导入‘http’模块。
用法:
const http = require('http');
用法:
response.addTrailers(headers);
参数:该属性接受上述和以下描述的单个参数:
- headers<String>:它接受响应的HTTP尾部标头(标头,但位于消息末尾)的名称。
返回值:它不返回任何值,而是按如下所述设置标头。
以下示例说明了Node.js中response.addTrailers()属性的用法。
范例1: 文件名:index.js
// Node.js program to demonstrate the
// response.addTrailers() Method
// Importing http module
var http = require('http');
// Setting up PORT
const PORT = process.env.PORT || 3000;
// Creating http Server
var httpServer = http.createServer(
function(request, response) {
// Setting up Headers
response.setHeader('Alfa', 'Beta');
response.writeHead(200, {
'Content-Type':'text/plain',
'Trailer':'Content-MD5'
});
response.addTrailers({'Content-MD5':
'7895bf4b8828b55ceaf47747b4bca667'});
console.log(response.getHeaders());
response.end('Trailer Added, ok');
});
// Listening to http Server
httpServer.listen(PORT, () => {
console.log(
"Server is running at port 3000...");
});
现在在浏览器中运行http://localhost:3000 /。
输出:在控制台中
>> Server is running at port 3000…
>> [Object:null prototype] {
alfa:’Beta’,
‘content-type’:’text/plain’,
trailer:‘Content-MD5’}
输出:在浏览器中
Trailer Added, ok
范例2: 文件名:index.js
Javascript
// Node.js program to demonstrate the
// response.addTrailers() Method
// Importing http module
var http = require('http');
// Setting up PORT
const PORT = process.env.PORT || 3000;
// Creating http Server
var httpServer = http.createServer(
function(req, response) {
// Setting up Headers
response.setHeader('Alfa1', '');
response.setHeader('Cookie-Setup',
['Alfa=Beta', 'Beta=Romeo']);
response.writeHead(200, {
'Content-Type':'text/plain',
'Trailer':'Content-MD5'
});
// addTrailers Content-MD5
response.addTrailers({'Content-MD5':
'7895bf4b8828b55ceaf47747b4bca667'});
// Adding Cookie-Setup as trailer
// ( Not gets added as trailer )
response.addTrailers({ 'Cookie-Setup':
['Alfa=Beta', 'Beta=Romeo'] });
// Checking and printing the headers
console.log("Calling trailer by getHeader:",
response.getHeader('Content-MD5'));
// console.log("Calling trailer by getHeader:",
response);
// Getting the set Headers
const headers = response.getHeaders();
// Printing those headers
// Header
console.log("Printing _header:", response._header);
// Trailer
console.log("Printing _trailer:", response._trailer);
// All headers
console.log("Printing All headers:", headers);
var Output = "Hello Geeksforgeeks..., "
+ "Available headers and trailers are:"
+ JSON.stringify(headers);
// Prints Output on the browser in response
response.write(Output);
response.end('ok');
});
// Listening to http Server
httpServer.listen(PORT, () => {
console.log("Server is running at port 3000...");
});
使用以下命令运行index.js文件:
node index.js
输出:
In Console
Server is running at port 3000…
>> Calling trailer by getHeader:undefined
>> Printing _header:HTTP/1.1 200 OK
Alfa1:
Cookie-Setup:Alfa=Beta
Cookie-Setup:Beta=Romeo
Content-Type:text/plain
Trailer:Content-MD5
Date:Wed, 09 Sep 2020 06:03:32 GMT
Connection:keep-alive
Transfer-Encoding:chunked
>> Printing _trailer:Cookie-Setup:Alfa=Beta, Beta=Romeo
>> Printing All headers:[Object:null prototype] {
alfa1:”,
‘cookie-setup’:[ ‘Alfa=Beta’, ‘Beta=Romeo’ ],
‘content-type’:‘text/plain’,
trailer:‘Content-MD5’}
现在在浏览器中运行http://localhost:3000 /。
输出:在浏览器中
Hello Geeksforgeeks…, Available headers and trailers are:{“alfa1″:””, “cookie-setup”:[“Alfa=Beta”, “Beta=Romeo”],
“content-type”:”text/plain”, “trailer”:”Content-MD5″}ok
参考: https://nodejs.org/api/http.html#http_response_addtrailers_headers
相关用法
- Node.js console.timeLog()用法及代码示例
- Node.js fs.fsyncSync()用法及代码示例
- Node.js process.nextTick()用法及代码示例
- Node.js GM charcoal()用法及代码示例
- Node.js GM blur()用法及代码示例
- Node.js GM sharpen()用法及代码示例
- Node.js GM drawLine()用法及代码示例
- Node.js GM drawArc()用法及代码示例
- Node.js GM drawPolyline()用法及代码示例
- Node.js GM drawBezier()用法及代码示例
- Node.js GM drawCircle()用法及代码示例
注:本文由纯净天空筛选整理自vikas_g大神的英文原创作品 Node.js response.addTrailers() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。