Node.js response.addTrailers() 方法
response.addTrailers() (在 v0.3.0 中添加)方法是“ http ”模块的内置应用程序编程接口,它向响应添加 HTTP 尾随标头(标头但位于消息末尾)。只有当分块编码用于响应时,才会发出预告片;如果不是(例如,如果请求是 HTTP/1.0 ),它们将被静默丢弃。 HTTP 要求发送 Trailer 标头以发出预告片,并在其值中包含标头字段列表。
当使用response.setHeader()设置了 headers 时,它们将与传递给 response.writeHead() 的任何 headers 合并,并且传递给response.writeHead ()的 headers 具有优先级。
为了得到响应和正确的结果,我们需要导入' 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...");
});
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。