Open In App

Express res.end() Function

Last Updated : 08 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 employed to promptly conclude the response without including any data.

Syntax: 

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

Parameters: The default encoding is ‘utf8‘ and the data parameter is the data with which the user wants to end the response.

Return Value: It returns an Object.

res.end() function in Express.js is basically used to end a response process.

Steps to create the Express App and Installing the Modules:

Step 1: Initializing the Nodejs app using the below command:

npm init -y

Step 2: Installing the express module:

npm install express

Step 2: After that, you can 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

The updated dependencies in package.json file will look like:

"dependencies": {
"express": "^4.18.2",

Example 1: Below is the code example of the res.status() 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: 

Server listening on PORT 3000

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

JavaScript
const express = require('express');
const app = express();
const PORT = 3000;

// With middleware
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

resendGIF

Output

We have a Cheat Sheet on Express Response methods where we covered all the important topics. To check those please go through Express.js Response Complete Reference article



Next Article

Similar Reads