Open In App

Express res.end() Function

Last Updated : 18 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
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.

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads