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 at
2 min read
How to Generate or Send JSON Data at the Server Side using Node.js ? In modern web development, JSON (JavaScript Object Notation) is the most commonly used format for data exchange between a server and a client. Node.js, with its powerful runtime and extensive ecosystem, provides robust tools for generating and sending JSON data on the server side. This guide will wa
3 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 javascri
5 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 a Simple Server Using ExpressJS? 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 simp
3 min read
How to Generate/Send JSON Data at the Client Side ? Javascript Object Notation (JSON) is a widely used format for sending and receiving data to or from the server. In this article, we will use fetch API to send and receive data from the NodeJS server. Advantages of JSON: Because of its easy and small syntax, it executes the response in a faster way.
3 min read
How to intercept response.send() / response.json() in Express JS In the context of Express , "intercept" usually refers to the process of capturing or modifying a request or response in a middleware function before it reaches its final destination (e.g., a route handler) or before it is sent back to the client.In Express, intercepting response.send() or response.
3 min read
Explain the use of req and res objects in Express JS Express JS is used to build RESTful APIs with Node.js. We have a 'req' (request) object in Express JS which is used to represent the incoming HTTP request that consists of data like parameters, query strings, and also the request body. Along with this, we have 'res' (response) which is used to send
4 min read
Working with forms using Express.js in Node.js In this article, we will be working with forms using ExpressJS in NodeJS.Using server side programming in Node.js, we can create forms where we can put certain parameters which upon filling gets stored in the database.Setting up environment:You can refer to this website for downloading Node.js. Alon
3 min read
Node.js Server Side Rendering (SSR) using EJS Server-side rendering (SSR) is a popular technique for rendering a normally client-side-only single-page app (SPA) on the server and then sending a fully rendered page to the client. The client's JavaScript bundle can then take over and the SPA can operate as normal. SSR technique is helpful in situ
3 min read