Learn how to receive information from clients, read URL parameters, access form and JSON data, send responses, and use HTTP status codes in Express.js applications.
req, res, request methods, route parameters,
query parameters, request bodies, response methods, headers,
status codes, and practical examples.
A request is a message sent by a client, such as a browser, mobile application, or API client, to a server.
A request may contain:
GET /students/101 HTTP/1.1
Host: localhost:3000
This request asks the server to retrieve information associated with student ID 101.
A response is the message returned by the server after processing a client request.
A response may contain:
HTTP/1.1 200 OK
Content-Type: text/plain
Student found successfully
The status code 200 indicates that the request was successful.
In Express.js route handlers, req represents the
request object and res represents the response object.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello Student!');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
req provides information about the incoming request.res sends a response back to the client.res.send() sends the response content.
You can inspect the HTTP method and URL using
req.method and req.url.
const express = require('express');
const app = express();
app.use((req, res, next) => {
console.log('Method:', req.method);
console.log('URL:', req.url);
next();
});
app.get('/', (req, res) => {
res.send('Home Page');
});
app.listen(3000, () => {
console.log('Server started');
});
When you visit the home page, the terminal logs:
Method: GET
URL: /
Route parameters capture values from the URL path.
Express makes them available through req.params.
const express = require('express');
const app = express();
app.get('/students/:id', (req, res) => {
const studentId = req.params.id;
res.send('Student ID: ' + studentId);
});
app.listen(3000, () => {
console.log('Server started');
});
Visit:
http://localhost:3000/students/101
Query parameters are values included in the URL after a question mark. They are commonly used for searches and filters.
Example URL:
http://localhost:3000/search?name=Amit&course=Node.js
const express = require('express');
const app = express();
app.get('/search', (req, res) => {
const name = req.query.name;
const course = req.query.course;
res.json({
studentName: name,
studentCourse: course
});
});
app.listen(3000, () => {
console.log('Server started');
});
Expected response:
{
"studentName": "Amit",
"studentCourse": "Node.js"
}
The request body contains data submitted by a client. For example, a student registration form might send a student's name, email, and course.
const express = require('express');
const app = express();
app.use(express.json());
app.post('/students', (req, res) => {
const student = req.body;
res.json({
message: 'Student received',
data: student
});
});
app.listen(3000, () => {
console.log('Server started');
});
Send this JSON body in a POST request:
{
"name": "Amit",
"course": "Node.js"
}
Example response:
{
"message": "Student received",
"data": {
"name": "Amit",
"course": "Node.js"
}
}
Use Postman or another HTTP client to send the POST
request with the Content-Type: application/json
header.
HTML forms can submit URL-encoded data. Use
express.urlencoded() to parse this type of request body.
const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: false }));
app.post('/register', (req, res) => {
const name = req.body.name;
const course = req.body.course;
res.send(
'Welcome ' + name + '. Your course is ' + course
);
});
app.listen(3000, () => {
console.log('Server started');
});
The form should submit fields named name
and course using the POST method.
Use res.send() to send a text or HTML response.
app.get('/message', (req, res) => {
res.send('Welcome to Soopro Pathshala');
});
Visiting /message displays:
Use res.json() to send JSON data. This is common
when building REST APIs for websites and mobile applications.
app.get('/student', (req, res) => {
res.json({
id: 101,
name: 'Rahul',
course: 'Node.js'
});
});
Response:
{
"id": 101,
"name": "Rahul",
"course": "Node.js"
}
An HTTP status code indicates the result of processing a request.
Express lets you set the status using res.status().
| Status Code | Meaning | Common Use |
|---|---|---|
| 200 | OK | Request succeeded. |
| 201 | Created | A resource was created successfully. |
| 400 | Bad Request | The request contains invalid data. |
| 401 | Unauthorized | Authentication is required or invalid. |
| 403 | Forbidden | The client is not permitted to access the resource. |
| 404 | Not Found | The requested resource was not found. |
| 500 | Internal Server Error | An unexpected server error occurred. |
app.post('/students', (req, res) => {
res.status(201).json({
message: 'Student created successfully'
});
});
The status code 201 is commonly used after a resource has been created successfully.
Response headers provide metadata about the server response.
You can set a header using res.set().
app.get('/headers', (req, res) => {
res.set('X-App-Name', 'Soopro Pathshala');
res.send('Custom response header added');
});
The response includes a custom header named
X-App-Name.
The res.redirect() method redirects a client
to another URL.
app.get('/old-page', (req, res) => {
res.redirect('/about');
});
app.get('/about', (req, res) => {
res.send('About Page');
});
When the client visits /old-page,
Express redirects it to /about.
| Property or Method | Purpose |
|---|---|
req.method |
Reads the HTTP method. |
req.url |
Reads the request URL. |
req.params |
Reads route parameters. |
req.query |
Reads query parameters. |
req.body |
Accesses parsed request-body data. |
req.get() |
Reads a request header. |
res.send() |
Sends text or HTML content. |
res.json() |
Sends JSON data. |
res.status() |
Sets the HTTP status code. |
res.set() |
Sets response headers. |
res.redirect() |
Redirects the client. |
Create an Express application with the following requirements:
/ that sends a welcome message./students/:id that reads a student ID./search?name=Amit that reads a query parameter.express.json() to parse JSON request bodies./students that receives student data.res.json().Select the correct answer for each question.