- Spec files
npm-express
Describes: pkg:npm/express@5.1.x
- Description
- Fast, unopinionated, minimalist web framework for Node.js
- Author
- tessl
- Last updated
index.md docs/
1# Express23Express is a fast, unopinionated, minimalist web framework for Node.js. It provides a robust set of features for web and mobile applications, including HTTP utilities, middleware support, routing, and template engine integration.45## Package Information67- **Package Name**: express8- **Package Type**: npm9- **Language**: JavaScript10- **Installation**: `npm install express`1112## Core Imports1314```javascript15const express = require('express');16```1718For ES modules:1920```javascript21import express from 'express';22```2324## Basic Usage2526```javascript27const express = require('express');28const app = express();2930// Middleware31app.use(express.json());32app.use(express.static('public'));3334// Routing35app.get('/', (req, res) => {36res.send('Hello World!');37});3839app.post('/users', (req, res) => {40res.json({ message: 'User created', data: req.body });41});4243// Start server44app.listen(3000, () => {45console.log('Server running on port 3000');46});47```4849## Architecture5051Express is built around several core concepts:5253- **Application**: The main Express application instance that handles HTTP requests54- **Middleware**: Functions that process requests and responses in a pipeline55- **Routing**: URL pattern matching and HTTP method handling56- **Request/Response Objects**: Enhanced Node.js IncomingMessage and ServerResponse objects57- **Template Engine Integration**: Support for various view engines like EJS, Handlebars, Pug5859## Capabilities6061### Application Management6263Core application creation, configuration, and server management functionality. Handles application settings, middleware mounting, and HTTP server lifecycle.6465```javascript { .api }66/**67* Creates an Express application instance68* @returns {Function} Express application function69*/70function express();7172/**73* Application prototype with routing and middleware methods74*/75interface Application extends Function {76// Settings77set(setting: string, val: any): this;78get(setting: string): any;79enable(setting: string): this;80disable(setting: string): this;81enabled(setting: string): boolean;82disabled(setting: string): boolean;8384// Server lifecycle85listen(port?: number, hostname?: string, callback?: Function): Server;8687// Middleware and routing88use(...handlers: Handler[]): this;89use(path: string, ...handlers: Handler[]): this;90route(path: string): Route;91}9293/**94* Express prototypes - accessible for extending functionality95*/96express.application: Application; // Application prototype97express.request: Request; // Request prototype98express.response: Response; // Response prototype99100/**101* Express constructors102*/103express.Route: typeof Route; // Route constructor104express.Router: typeof Router; // Router constructor105```106107[Application Management](./application.md)108109### HTTP Request Processing110111Request object enhancements providing access to headers, query parameters, content negotiation, and request metadata analysis.112113```javascript { .api }114/**115* Enhanced request object with Express-specific properties and methods116*/117interface Request extends IncomingMessage {118// Header access119get(name: string): string | undefined;120header(name: string): string | undefined;121122// Content negotiation123accepts(types: string | string[]): string | false;124acceptsEncodings(encodings: string | string[]): string | false;125acceptsCharsets(charsets: string | string[]): string | false;126acceptsLanguages(langs: string | string[]): string | false;127128// Request analysis129is(type: string | string[]): string | false;130131// Properties132query: { [key: string]: any };133params: { [key: string]: string };134body: any;135protocol: string;136secure: boolean;137ip: string;138hostname: string;139path: string;140fresh: boolean;141stale: boolean;142xhr: boolean;143}144```145146[Request Processing](./request.md)147148### HTTP Response Generation149150Response object enhancements for sending various types of responses, setting headers, managing cookies, and handling redirects.151152```javascript { .api }153/**154* Enhanced response object with Express-specific methods155*/156interface Response extends ServerResponse {157// Response sending158send(body?: any): this;159json(obj: any): this;160jsonp(obj: any): this;161sendStatus(statusCode: number): this;162sendFile(path: string, options?: object, callback?: Function): void;163164// Headers and status165status(code: number): this;166set(field: string, val: string | string[]): this;167get(field: string): string | undefined;168169// Cookies170cookie(name: string, val: any, options?: object): this;171clearCookie(name: string, options?: object): this;172173// Redirects174redirect(url: string): void;175redirect(status: number, url: string): void;176}177```178179[Response Generation](./response.md)180181### Routing System182183Router and Route classes for organizing application endpoints, handling HTTP methods, and managing route parameters.184185```javascript { .api }186/**187* Router constructor for creating modular route handlers188* @param {object} options - Router configuration options189* @returns {Router} Router instance190*/191function Router(options?: object): Router;192193/**194* Route constructor for individual route management195* @param {string} path - Route path pattern196* @returns {Route} Route instance197*/198function Route(path: string): Route;199```200201[Routing System](./router.md)202203### Built-in Middleware204205Express includes several built-in middleware functions for common web application needs including body parsing and static file serving.206207```javascript { .api }208// Body parsing middleware209express.json(options?: object): Function;210express.urlencoded(options?: object): Function;211express.raw(options?: object): Function;212express.text(options?: object): Function;213214// Static file serving215express.static(root: string, options?: object): Function;216```217218[Built-in Middleware](./middleware.md)219220## Types221222```javascript { .api }223/**224* Generic middleware function signature225*/226type Handler = (req: Request, res: Response, next: NextFunction) => void;227228/**229* Next function for middleware chain continuation230*/231type NextFunction = (err?: any) => void;232233/**234* HTTP Server instance from Node.js235*/236interface Server {237listen(port?: number, hostname?: string, callback?: Function): this;238close(callback?: Function): this;239}240```