In Express.js, the "res" object, representing the HTTP response, is used to send data back to the client.
In an Express.js application, sending a response to a client is a fundamental operation. Express provides various methods to send responses back to clients, such as sending plain text, HTML, JSON, or files.
We can send Text responses from the server to the client machine.
const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; app.get('/', (req, res) => { res.send('Hello, World!'); }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; app.get('/send-html', (req, res) => { res.send('<h1>Hi, Welcome to the Express.js world!</h1>'); }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
We can send HTML responses from the server to the client machine using the `res.send()` method by passing HTML markup as a string.
const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; app.get('/json', (req, res) => { const data = { message: 'Hello, JSON!' }; res.json(data); }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
We can send JSON responses from the server to the client machine. This method automatically sets the `Content-Type` header to `application/json`.
const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; app.get('/notfound', (req, res) => { res.status(404).send('Not Found'); }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; app.get('/redirect', (req, res) => { res.redirect('https://www.instanests.com'); }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
We can set the HTTP status code for the response using the "res.status()" method.
const path = require('path'); const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; app.get('/file', (req, res) => { const filePath = path.join(__dirname, 'path/to/file.txt'); res.sendFile(filePath); }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
Express provides methods for handling redirection.
We can send files as responses using "res.sendFile()".
const path = require('path'); const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000; app.get('/', (req, res) => { res.send('Hello, World!'); }); app.get('/send-html', (req, res) => { res.send('<h1>Hi, Welcome to the Express.js world!</h1>'); }); app.get('/file', (req, res) => { const filePath = path.join(__dirname, 'path/to/file.txt'); res.sendFile(filePath); }); app.get('/redirect', (req, res) => { res.redirect('https://www.instanests.com'); }); app.get('/json', (req, res) => { const data = { message: 'Hello, JSON!' }; res.json(data); }); app.get('/notfound', (req, res) => { res.status(404).send('Not Found'); }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
// navigate to folder location where server.js exist node server.js