Node.js response.addTrailers() Method
The response.addTrailers() (Added in v0.3.0) method is an inbuilt application programming interface of the ‘http‘ module which adds HTTP trailing headers (a header but at the end of the message) to the response. Trailers will only be emitted if the chunked encoding is used for the response; if it is not (e.g. if the request was HTTP/1.0), they will be silently discarded. HTTP requires the Trailer header to be sent in order to emit trailers, with a list of the header fields in its value.
When headers have been set with response.setHeader(), they will be merged with any headers passed to response.writeHead(), with the headers passed to response.writeHead() given precedence.
In order to get a response and a proper result, we need to import ‘http’ module.
Syntax:
const http = require('http');
Syntax:
response.addTrailers(headers);
Parameters: This property accepts a single parameter as mentioned above and described below:
- headers<String>: It accepts the name of the HTTP trailing headers (a header but at the end of the message) to the response.
Return Value: It does not return any value, instead sets a header as described below.
The below example illustrates the use of response.addTrailers() property in Node.js.
Example 1: Filename: index.js
// Node.js program to demonstrate the // response.addTrailers() Method // Importing http modulevar http = require('http'); // Setting up PORTconst PORT = process.env.PORT || 3000; // Creating http Servervar 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 ServerhttpServer.listen(PORT, () => { console.log( "Server is running at port 3000...");}); |
Now run http://localhost:3000/ in the browser.
Output: In Console
>> Server is running at port 3000…
>> [Object: null prototype] {
alfa:’Beta’,
‘content-type’:’text/plain’,
trailer: ‘Content-MD5’}
Output: In Browser
Trailer Added, ok
Example 2: Filename: index.js
Javascript
// Node.js program to demonstrate the // response.addTrailers() Method // Importing http modulevar http = require('http'); // Setting up PORTconst PORT = process.env.PORT || 3000; // Creating http Servervar 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 ServerhttpServer.listen(PORT, () => { console.log("Server is running at port 3000...");}); |
Run index.js file using the following command:
node index.js
Output:
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’}
Now run http://localhost:3000/ in the browser.
Output: In Browser
Hello Geeksforgeeks…, Available headers and trailers are:{“alfa1″:””, “cookie-setup”:[“Alfa=Beta”, “Beta=Romeo”],
“content-type”:”text/plain”, “trailer”:”Content-MD5″}ok
Reference: https://nodejs.org/api/http.html#http_response_addtrailers_headers
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.


