Express res.end() Function - GeeksforGeeks

Express res.end() Function

Last Updated : 15 Sep, 2026

The res.end() function concludes the response process and is derived from the HTTP.ServerResponse's response.end() method in the Node core. It is used to end the response, with or without data.

frame_3480

Syntax: 

res.end([data[, encoding]][, callback])

Parameters:

  • data: The optional data to write before ending the response.
  • encoding: The encoding used when data is a string.
  • callback: An optional function called when the response has finished.

Return Value: Returns the response object (res).

Installation of the express module:

You can visit the link to Install the express module. You can install this package by using this command.

npm install express

After installing the express module, you can check your express version in the command prompt using the command.

npm version express

After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js


Project Structure:

NodeProj

Example 1: Below is the code example of the res.end() Method:

JavaScript
const express = require('express');
const app = express();
const PORT = 3000;
// Without middleware
app.get('/',
    function (req, res) {
        res.end();
    });
app.listen(PORT,
    function (err) {
        if (err) console.log(err);
        console.log("Server listening on PORT", PORT);
    });

Steps to run the program: 

node index.js

Output: 

Screenshot-2026-08-17-124132


Example 2: Below is the code example of the res.end() Method:

JavaScript
const express = require('express');
const app = express();
const PORT = 3000;
app.use('/user', function (req, res, next) {
    console.log("/user called");
    res.end();
});
app.get('/user', function (req, res) {
    console.log("User Page");
    res.end();
});
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});

Steps to run the program:

node index.js

Output: go to http://localhost:3000/user, then you will see the following output on your console

Screenshot-2026-08-17-145749

Working of res.end()

  • An Express application receives a client request.
  • The route handler or middleware gets access to the response object through res.
  • res.end() signals that the response is complete.
  • Any optional response data is written before the response is ended.
  • After the response is ended, no additional response data should be sent.

Use Cases of res.end()

  • Ending an HTTP response without sending a response body.
  • Completing a response after writing data manually.
  • Ending responses inside middleware or route handlers.
  • Handling requests where no response content is required.
  • Explicitly terminating the response when the server has finished sending data.
Comment