當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js response.addTrailers()用法及代碼示例


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

相關用法


注:本文由純淨天空篩選整理自vikas_g大神的英文原創作品 Node.js response.addTrailers() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。