Node.js http.ServerResponse.end() Method

25 Dec, 2020
The httpServerResponse.end() is an inbuilt application programming interface of class Server Response within http module which is used to send the signal to the server that all the header has been sent.
Syntax:
response.end(data, Encodingtype, Callbackfunction)
Parameters: This method takes three Parameters
Data: Chunk of data that has to be sent
Encoding Type: Type encoding for the data
Callback: Callback function for further operation if necessary.
Return Value: This method returns this Server Response object.
Example 1: Filename: index.js
Javascript
// Node.js program to demonstrate the  // response.end() 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){     // Getting connection by using  // response.connection method  const value = response.connection;      // Ending the response  response.end( "port address : " +   value.address().port, 'utf8', () => {       console.log("displaying the result...");         // Closing the server      httpServer.close(()=>{          console.log("server is closed")      })  }); });     // Listening to http Server httpServer.listen(PORT, () => {     console.log("Server is running at port 3000..."); });
Execution command:
node index.js
Console output:
Server is running at port 3000...
displaying the result...
displaying the result...
server is closed
server is closed
Browser Output:
Paste the localhost address http://localhost:3000/. In the search bar of the browser.
port address : 3000
Example 2: Filename: index.js
Javascript
// Node.js program to demonstrate the  // response.end() method    // Importing http module var http = require('http');   // Request and response handler const http2Handlers = (request, response) => {       // Getting connection   // by using response.connection Api  const value = response.connection;      // Display result by using end()  // api and ending the response  response.end( "family  : " +   value.address().family, 'utf8', () => {       console.log("displaying the result...");         httpServer.close(()=>{          console.log("server is closed")      })  });  };      // Creating http Server  and listening// on the 3000 portvar httpServer = http.createServer(    http2Handlers).listen(3000, () => {     console.log("Server is running at port 3000..."); });
Execution command:
node index.js
Console output:
Server is running at port 3000...
displaying the result...
displaying the result...
server is closed
server is closed
Browser Output:
Paste the localhost address http://localhost:3000/. In the search bar of the browser.
family  : IPv6
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_response_end_data_encoding_callback
Node.js-Methods
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
JWT Authentication with Node.js
Installation of Node.js on Windows
Difference between dependencies, devDependencies and peerDependencies
Mongoose Populate() Method
Mongoose find() Function
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Differences between Functional Components and Class Components in React