How to Send Response From Server to Client using Node.js and Express.js ?
Last Updated :
14 Jun, 2024
In web development, sending responses from the server to the client is a fundamental aspect of building interactive and dynamic applications. Express.js, a popular framework for Node.js, simplifies this process, making it easy to send various types of responses such as HTML, JSON, files, and more. This article will guide you through the process of sending different types of responses from a server to a client using Node.js and Express.js.
Prerequisite:
Installation Steps
Step 1: Create a new directory for your project and initialize it with npm:
mkdir myapp
cd myapp
npm init -y
Step 2: Install the necessary packages/libraries in your project using the following commands.
npm install express
Project structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"express": "^4.19.2",
}
Sending Different Types of Responses
1. Sending Plain Text
You can send plain text using the res.send
method. This method is versatile and automatically sets the Content-Type
header to text/plain
for strings.
app.get('/text', (req, res) => {
res.send('Hello, this is a plain text response!');
});
2. Sending HTML
To send HTML content, you can also use res.send
. Express will set the Content-Type
to text/html
when it detects HTML content.
app.get('/html', (req, res) => {
res.send('<h1>Hello, this is an HTML response!</h1><p>Welcome to the world of Express.js.</p>');
});
3. Sending JSON
Sending JSON data is straightforward with res.json
. This method sets the Content-Type
header to application/json
automatically and stringifies the object.
app.get('/json', (req, res) => {
res.json({ message: 'Hello, this is a JSON response!', status: 'success' });
});
4. Sending a File
To send a file, use the res.sendFile
method. This method requires an absolute path to the file you want to send.
const path = require('path');
app.get('/file', (req, res) => {
res.sendFile(path.join(__dirname, 'example.txt'));
});
Sending Status Codes
You can set custom status codes using res.status
. This method allows you to send a specific HTTP status code along with your response.
app.get('/status', (req, res) => {
res.status(404).send('Page not found');
});
Example 1: Demonstrating the use of the status() function.
Node
// Filename - index.js
const express = require('express');
const app = express();
app.get('/' , (req,res)=>{
// 200 status code means OK
res.status().send(200);
})
// Server setup
app.listen(4000 , ()=>{
console.log("server running");
});
Step to Run Application: Run the application using the following command from the root directory of the project
node index.js
Output: Now open your browser and go to http://localhost:4000/, you will see the following output:

Example 2: Sending some particular data to the client then you can use send() function.
Node
// index.js
const express = require('express');
const app = express();
var computerSciencePortal = "GeeksforGeeks";
app.get('/' , (req,res)=>{
// Server will send GeeksforGeeks as response
res.send(computerSciencePortal);
})
// Server setup
app.listen(4000 , ()=>{
console.log("server running");
});
Step to Run Application: Run the application using the following command from the root directory of the project
node index.js
Output: Now open your browser and go to http://localhost:4000/, you will see the following output:

Example 3: Sending the JSON response from the server to the client using json() function.
Node
// index.js
const express = require('express');
const app = express();
// Sample JSON data
var data = {
portal : "GeeksforGeeks",
knowledge : "unlimited",
location : "Noida"
}
app.get('/' , (req,res)=>{
// This will send the JSON data to the client.
res.json(data);
})
// Server setup
app.listen(4000 , ()=>{
console.log("server running");
});
Step to Run Application: Run the application using the following command from the root directory of the project
node index.js
Output: Now open your browser and go to http://localhost:4000/, you will see the following output:

So, These are the methods that you can use to send responses from server to client using node and express.
Key Points Recap:
- Use
res.send
for plain text or HTML. - Use
res.json
for JSON responses. - Use
res.sendFile
to send files. - Use
res.redirect
to redirect the client. - Use
res.status
to send specific HTTP status codes. - Handle errors and asynchronous operations appropriately.
- Organize routes and middleware for cleaner code.
Conclusion
Sending responses from a server to a client using Node.js and Express.js is a fundamental skill in web development. Express simplifies this task with a range of methods that handle various types of responses, from plain text and JSON to file streams and redirects. By understanding how to use these methods effectively, you can build dynamic and interactive web applications that provide a seamless user experience.
Similar Reads
How to send data from client side to Node.js server using Ajax without page reloading ?
In this article, we are learning about how can we send data to a node server using Ajax without reloading the page from the client-side. Approach: We are creating a button in HTML document on the client-side when the button is pressed a request is made on our node server and the object is received a
2 min read
How to Send JSON Response using Node.js ?
NodeJS is the runtime environment, which can execute the javascript code on any platform. It is widely used to create and run web application servers because of its salient features. During production, several times we need to send the resources or some type of information as a response, and javascr
5 min read
How to set response header on Express JS assets?
In Express.js applications, we can set the response headers on assets to improve performance and also control the cache behavior. In this article, we will set the response header on Express.js assets by using 2 different approaches. We will see the practical implementation of this approach in terms
3 min read
How to create a simple server using Express JS?
The server plays an important role in the development of the web application. It helps in managing API requests and communication between the client and the backend. ExpressJS is the fast and famous framework of the Node.Js which is used for creating the server. In this article, we will create a sim
3 min read
How To Make A GET Request using Postman and Express JS
Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. In this tutorial, we will see how To Make A GET Request using Postman and Express JS PrerequisitesNode JSExpress JSPostmanTable of Content What is GET Request?Steps to make a GET Request
3 min read
How to create routes using Express and Postman?
In this article we are going to implement different HTTP routes using Express JS and Postman. Server side routes are different endpoints of a application that are used to exchange data from client side to server side. Express.js is a framework that works on top of Node.js server to simplify its APIs
3 min read
How To Render Dynamic Lists Using EJS and Express ?
Rendering of dynamic lists in EJS and Express mainly allows us to dynamically generate the HTML content which is based on the data from the backend server. We can render dynamic lists using the '<% %>' tags. Steps to Render Dynamic ListsStep 1: Create a directory for the projectmkdir rootcd ro
2 min read
How to create different post request using Node.js ?
A POST request is one of the important requests in all HTTP requests. This request is used for storing the data on the WebServer. For Eg File uploading is a common example of a post request. There are many approached to perform an HTTP POST request in Node.js. Various open-source libraries are also
3 min read
How to Separate Routers and Controllers in Node.js ?
In a Node.js application, especially when using the Express framework, separating routers and controllers is a common practice to keep the codebase organized, maintainable, and scalable. This separation of concerns ensures that routing logic is kept separate from the business logic, making the appli
4 min read
Real-Time Auction Platform using Node and Express.js
The project is a Real-Time Auction Platform developed using Node.js Express.js and MongoDB database for storing details where users can browse different categories of products, view ongoing auctions, bid on items, and manage their accounts. The platform also allows sellers to list their products for
12 min read