Define method-specific routes
Build an HTTP API
Express.js · Interface
The Express application object, conventionally called app, is created with express(). It provides the interface for defining routes, configuring middleware, and handling HTTP requests.
An application is created by calling the top-level express() function. The resulting app object exposes routing and configuration methods, can be referenced through req.app and res.app, and can be passed to a Node.js HTTP server to handle requests.
const app = express();app.express() function to create the application object.req.app or res.app.app.METHOD() names to route requests by HTTP method.app.all() with a normal HTTP-method route: it applies middleware for every request method at its path.import express, { type Request, type Response } from 'express';
const app = express();
app.get('/health', (_req: Request, res: Response) => {
res.send('ok');
});
app.listen(3000);Creates an application, registers a GET route, and starts listening on port 3000.
Build an HTTP API
Apply cross-cutting request processing
Expose reusable route groups
Run the service
The application object is the app value returned by calling the top-level express() function. It provides the methods used to define routes, configure middleware, and handle HTTP requests.
Follow-up: How can request handlers access the application? The same application is available as req.app and res.app.
app.METHOD() defines a route for the corresponding HTTP method, such as app.get() or app.post(). The method name is written in lowercase even though it represents an uppercase HTTP method.
Follow-up: When should app.all() be used? Use it when middleware must run for all HTTP methods at a specified path rather than only one method.
An application can be passed to Node's HTTP server because the application object acts as the request handler. Calling app.listen() starts listening for connections using the application.
Follow-up: What is the practical benefit? The same application handler can support both HTTP and HTTPS server setups.
express()app.all()app.METHOD()app.use()app.route()app.param()req.appres.app