- Spec files
npm-express
Describes: pkg:npm/express@4.21.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 that provides a robust set of features for building single-page, multi-page, and hybrid web applications. It serves as the de facto standard web server framework for Node.js, offering a thin layer of fundamental web application features without obscuring Node.js features.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// Basic route31app.get('/', (req, res) => {32res.send('Hello World!');33});3435// Start server36app.listen(3000, () => {37console.log('Server running on port 3000');38});39```4041## Architecture4243Express is built around several key components:4445- **Application Instance**: The main Express app created by `express()` function46- **Router System**: Modular route handlers for organizing endpoints47- **Middleware Stack**: Functions that execute during the request-response cycle48- **Request/Response Objects**: Enhanced versions of Node.js HTTP objects49- **Route Handling**: Pattern matching and HTTP method routing50- **Template Engine Integration**: Support for various view engines5152## Capabilities5354### Application Creation5556Core Express application factory and configuration methods for setting up web servers and APIs.5758```javascript { .api }59/**60* Creates an Express application instance61* @returns {Application} Express application instance62*/63function express(): Application;6465/**66* Express application instance with routing and middleware capabilities67*/68interface Application {69/** Set application setting */70set(setting: string, value: any): Application;71/** Get application setting */72get(setting: string): any;73/** Enable boolean setting */74enable(setting: string): Application;75/** Disable boolean setting */76disable(setting: string): Application;77/** Check if setting is enabled */78enabled(setting: string): boolean;79/** Check if setting is disabled */80disabled(setting: string): boolean;81/** Start HTTP server */82listen(port?: number, hostname?: string, callback?: () => void): Server;83}84```8586[Application Management](./application.md)8788### HTTP Routing8990Route definition and HTTP method handling for creating RESTful APIs and web applications.9192```javascript { .api }93/**94* HTTP method routing functions95*/96interface Application {97/** Handle GET requests */98get(path: string, ...handlers: RequestHandler[]): Application;99/** Handle POST requests */100post(path: string, ...handlers: RequestHandler[]): Application;101/** Handle PUT requests */102put(path: string, ...handlers: RequestHandler[]): Application;103/** Handle DELETE requests */104delete(path: string, ...handlers: RequestHandler[]): Application;105/** Handle PATCH requests */106patch(path: string, ...handlers: RequestHandler[]): Application;107/** Handle all HTTP methods */108all(path: string, ...handlers: RequestHandler[]): Application;109/** Create route instance for chaining */110route(path: string): Route;111}112113/**114* Request handler function signature115*/116type RequestHandler = (req: Request, res: Response, next: NextFunction) => void | Promise<void>;117```118119[HTTP Routing](./routing.md)120121### Middleware System122123Middleware functions for request processing, authentication, logging, and request/response modification.124125```javascript { .api }126/**127* Middleware mounting and built-in middleware functions128*/129interface Application {130/** Mount middleware at optional path */131use(path?: string, ...middleware: RequestHandler[]): Application;132/** Add parameter middleware */133param(name: string, handler: ParamHandler): Application;134}135136/**137* Built-in middleware functions138*/139declare const express: {140/** Parse JSON request bodies */141json(options?: JsonOptions): RequestHandler;142/** Parse URL-encoded request bodies */143urlencoded(options?: UrlencodedOptions): RequestHandler;144/** Parse raw request bodies */145raw(options?: RawOptions): RequestHandler;146/** Parse text request bodies */147text(options?: TextOptions): RequestHandler;148/** Serve static files */149static(root: string, options?: StaticOptions): RequestHandler;150};151152type ParamHandler = (req: Request, res: Response, next: NextFunction, value: any, name: string) => void;153```154155[Middleware](./middleware.md)156157### Request Processing158159Enhanced request object with Express-specific properties and methods for handling incoming HTTP requests.160161```javascript { .api }162/**163* Express request object extends Node.js IncomingMessage164*/165interface Request {166/** Reference to Express app instance */167app: Application;168/** Base URL path */169baseUrl: string;170/** Request body (populated by body parsers) */171body: any;172/** Request cookies */173cookies: { [key: string]: string };174/** Request hostname */175hostname: string;176/** Remote IP address */177ip: string;178/** Original request URL */179originalUrl: string;180/** Route parameters */181params: { [key: string]: string };182/** Request path */183path: string;184/** Request protocol */185protocol: string;186/** Query string parameters */187query: { [key: string]: any };188/** Currently matched route */189route: Route;190/** Check if request is secure (HTTPS) */191secure: boolean;192/** Check if request is XMLHttpRequest */193xhr: boolean;194}195```196197[Request Processing](./request.md)198199### Response Handling200201Enhanced response object with Express-specific methods for sending responses, setting headers, and handling cookies.202203```javascript { .api }204/**205* Express response object extends Node.js ServerResponse206*/207interface Response {208/** Reference to Express app instance */209app: Application;210/** Response locals for template rendering */211locals: { [key: string]: any };212213/** Send response */214send(body?: any): Response;215/** Send JSON response */216json(obj?: any): Response;217/** Send status code */218sendStatus(statusCode: number): Response;219/** Send file */220sendFile(path: string, options?: SendFileOptions, callback?: (err?: Error) => void): void;221/** Redirect request */222redirect(status: number, url: string): void;223redirect(url: string): void;224/** Set status code */225status(statusCode: number): Response;226/** Set response header */227set(field: string | { [key: string]: string }, value?: string): Response;228/** Set cookie */229cookie(name: string, value: string, options?: CookieOptions): Response;230/** Clear cookie */231clearCookie(name: string, options?: CookieOptions): Response;232}233```234235[Response Handling](./response.md)236237### Router System238239Modular route handlers for organizing and grouping related routes with middleware support.240241```javascript { .api }242/**243* Create router instance for modular route handlers244* @param options Router configuration options245* @returns Router instance246*/247function Router(options?: RouterOptions): Router;248249/**250* Router instance with route handling capabilities251*/252interface Router {253/** Mount middleware */254use(path?: string, ...handlers: RequestHandler[]): Router;255/** Handle all HTTP methods */256all(path: string, ...handlers: RequestHandler[]): Router;257/** HTTP method handlers */258get(path: string, ...handlers: RequestHandler[]): Router;259post(path: string, ...handlers: RequestHandler[]): Router;260put(path: string, ...handlers: RequestHandler[]): Router;261delete(path: string, ...handlers: RequestHandler[]): Router;262patch(path: string, ...handlers: RequestHandler[]): Router;263/** Create route for chaining */264route(path: string): Route;265/** Add parameter middleware */266param(name: string, handler: ParamHandler): Router;267}268269interface RouterOptions {270/** Enable case-sensitive routing */271caseSensitive?: boolean;272/** Merge params from parent */273mergeParams?: boolean;274/** Enable strict routing */275strict?: boolean;276}277```278279[Router System](./router.md)280281## Core Types282283```javascript { .api }284/**285* Next function for middleware chain286*/287type NextFunction = (err?: Error) => void;288289/**290* Route object for individual routes291*/292interface Route {293/** Route path pattern */294path: string;295/** HTTP methods handled */296methods: { [method: string]: boolean };297/** Handle all HTTP methods */298all(...handlers: RequestHandler[]): Route;299/** HTTP method handlers */300get(...handlers: RequestHandler[]): Route;301post(...handlers: RequestHandler[]): Route;302put(...handlers: RequestHandler[]): Route;303delete(...handlers: RequestHandler[]): Route;304patch(...handlers: RequestHandler[]): Route;305}306307/**308* Error handling middleware signature309*/310type ErrorRequestHandler = (err: Error, req: Request, res: Response, next: NextFunction) => void;311```