Different Types of Responses You Can Send Using Express.js in Node.js (With Examples)

In Express.js (a web framework for Node.js), you can send various types of responses back to the client using different methods provided by the response object (res). In Express.js, you can send different types of responses such as plain text, HTML, JSON, or files using methods like 'res.send()', 'res.json()', and 'res.sendFile()'. These methods allow you to tailor responses based on the client’s request, making Express ideal for building flexible web applications.

1. Sending Text/HTML

You can send plain text or HTML content to the client.

				
					const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('<h1>Hello, World!</h1>'); // Sending HTML
});

app.listen(3000, () =&gt; {
    console.log('Server is running on port 3000');
});

				
			
2. Sending JSON

Express provides a built-in 'res.json()' method to send JSON responses.

				
					app.get('/json', (req, res) =&gt; {
    res.json({ message: 'Hello, JSON' }); // Sending a JSON object
});

				
			
3. Sending a File

You can send files directly using the 'res.sendFile()' method. This is useful for serving static files or file downloads.

				
					const path = require('path');

app.get('/file', (req, res) =&gt; {
    res.sendFile(path.join(__dirname, 'example.txt')); // Sending a file
});

				
			
4. Sending Status Code with Response

You can send a specific HTTP status code along with a response. This is useful for sending error responses or custom statuses.

				
					app.get('/not-found', (req, res) =&gt; {
    res.status(404).send('Page Not Found'); // Sending a 404 status with text
});

				
			
5. Redirecting

The 'res.redirect()' method allows you to redirect the client to another URL.

				
					app.get('/redirect', (req, res) =&gt; {
    res.redirect('/new-location'); // Redirecting to a new URL
});

				
			
6. Setting Headers

You can set custom headers using 'res.set()' or 'res.header()' before sending a response.

				
					app.get('/header', (req, res) =&gt; {
    res.set('Content-Type', 'application/json');
    res.send({ message: 'Custom Header Set' });
});

				
			
7. Sending Buffer

You can send raw binary data using a Buffer object.

				
					app.get('/buffer', (req, res) =&gt; {
    const buffer = Buffer.from('Hello Buffer', 'utf-8');
    res.send(buffer); // Sending buffer data
});

				
			
8. Sending Downloadable File

Using 'res.download()', you can send a file and trigger a download prompt in the client’s browser.

				
					app.get('/download', (req, res) =&gt; {
    res.download(path.join(__dirname, 'example.txt')); // File download
});

				
			
9. Chaining Status with Other Responses

You can chain status codes with other responses like 'json()' or 'send()'.

				
					app.get('/success', (req, res) =&gt; {
    res.status(200).json({ message: 'Success' }); // Sending JSON with status 200
});

				
			
10. Sending HTML Templates

You can use 'res.render()' to render HTML templates (e.g., with Pug, EJS) if you have a templating engine set up.

				
					app.set('view engine', 'pug');

app.get('/template', (req, res) =&gt; {
    res.render('index', { title: 'Hey', message: 'Hello from Pug' }); // Rendering a Pug template
});

				
			

Important Points :

  • Express.js supports various response types such as plain text, HTML, JSON, files, etc.
  • Use res.send() to send plain text or HTML.
  • Use res.json() to send JSON data.
  • Use res.sendFile() to send files.

Tailor server responses based on client request needs using these methods.