Express Application Initialization, Routing & Request/Response Objects0%
Express Application Initialization, Routing & Request/Response Objects
Beginner12 min read•Updated: Jul 11, 2026
Express Application Initialization, Routing & Request/Response Objects
The core workflow of any Express application revolves around three pillars: initializing the application instance, registering routes mapped to HTTP verbs, and manipulating the Request (req) and Response (res) objects.
1. Application Initialization and Server Lifecycle
An Express application begins by invoking the top-level express() function exported by the module:
JavaScript
import express from 'express';
const app = express();
const PORT = process.env.PORT || 4000;
// Register routes here...
const server = app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
// Graceful Shutdown handling
process.on('SIGTERM', () => {
console.log('SIGTERM signal received: closing HTTP server');
server.close(() => {
console.log('HTTP server closed cleanly');
});
});
2. HTTP Verb Routing Methods
Express supports all standard HTTP verbs:
JavaScript
// GET: Retrieve resource
app.get('/api/products', (req, res) => {
res.json({ products: [] });
});
// POST: Create resource
app.post('/api/products', (req, res) => {
res.status(201).json({ message: 'Product created' });
});
// PUT: Replace entire resource
app.put('/api/products/:id', (req, res) => {
res.json({ message: `Product ${req.params.id} fully updated` });
});
// PATCH: Partial update of resource
app.patch('/api/products/:id', (req, res) => {
res.json({ message: `Product ${req.params.id} partially modified` });
});
// DELETE: Remove resource
app.delete('/api/products/:id', (req, res) => {
res.status(204).send(); // 204 No Content
});
3. The req and res Ecosystem
Express decorates the native Node.js request and response objects with convenient helper properties and methods:
Key req Properties
req.method: The HTTP verb (GET, POST, etc.)req.path: The path part of the request URL (e.g.,/api/users)req.ip: Client IP addressreq.headers: Key-value map of incoming HTTP headersreq.get(headerName): Case-insensitive header lookup (e.g.,req.get('Authorization'))
Key res Methods
res.status(code): Sets the HTTP status code (chainable)res.json(data): Sends JSON response with appropriate headersres.send(body): Sends generic HTTP body (HTML, string, or Buffer)res.sendStatus(code): Sets status code and sends its text representation (e.g.,res.sendStatus(404)sends "Not Found")res.redirect(url): Performs HTTP 302/301 redirectionres.set(field, value): Sets an outgoing HTTP header
Interactive Quiz
Multiple Choice Questions
Question 1
Which HTTP status code should be returned when a new resource is successfully created via a POST request?
Question 2
What is the fundamental difference between HTTP PUT and HTTP PATCH methods in RESTful API design?
Question 3
Which method on the Express
res object sets the HTTP response code and returns the response in a chainable manner?Question 4
How can you retrieve the value of the
Authorization header safely regardless of whether the client sent it in upper or lowercase?Question 5
What does
res.status(204).send() communicate to the client?Next Lesson
Route Parameters, Query Strings & Request Body Parsing
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Native HTTP Server vs Express Framework Philosophy | Route Parameters, Query Strings & Request Body Parsing |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.