npm-express

Description
Fast, unopinionated, minimalist web framework for Node.js
Author
tessl
Last updated

How to use

npx @tessl/cli registry install tessl/npm-express@4.21.0

index.md docs/

1
# Express
2
3
Express 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.
4
5
## Package Information
6
7
- **Package Name**: express
8
- **Package Type**: npm
9
- **Language**: JavaScript
10
- **Installation**: `npm install express`
11
12
## Core Imports
13
14
```javascript
15
const express = require('express');
16
```
17
18
For ES modules:
19
20
```javascript
21
import express from 'express';
22
```
23
24
## Basic Usage
25
26
```javascript
27
const express = require('express');
28
const app = express();
29
30
// Basic route
31
app.get('/', (req, res) => {
32
res.send('Hello World!');
33
});
34
35
// Start server
36
app.listen(3000, () => {
37
console.log('Server running on port 3000');
38
});
39
```
40
41
## Architecture
42
43
Express is built around several key components:
44
45
- **Application Instance**: The main Express app created by `express()` function
46
- **Router System**: Modular route handlers for organizing endpoints
47
- **Middleware Stack**: Functions that execute during the request-response cycle
48
- **Request/Response Objects**: Enhanced versions of Node.js HTTP objects
49
- **Route Handling**: Pattern matching and HTTP method routing
50
- **Template Engine Integration**: Support for various view engines
51
52
## Capabilities
53
54
### Application Creation
55
56
Core Express application factory and configuration methods for setting up web servers and APIs.
57
58
```javascript { .api }
59
/**
60
* Creates an Express application instance
61
* @returns {Application} Express application instance
62
*/
63
function express(): Application;
64
65
/**
66
* Express application instance with routing and middleware capabilities
67
*/
68
interface Application {
69
/** Set application setting */
70
set(setting: string, value: any): Application;
71
/** Get application setting */
72
get(setting: string): any;
73
/** Enable boolean setting */
74
enable(setting: string): Application;
75
/** Disable boolean setting */
76
disable(setting: string): Application;
77
/** Check if setting is enabled */
78
enabled(setting: string): boolean;
79
/** Check if setting is disabled */
80
disabled(setting: string): boolean;
81
/** Start HTTP server */
82
listen(port?: number, hostname?: string, callback?: () => void): Server;
83
}
84
```
85
86
[Application Management](./application.md)
87
88
### HTTP Routing
89
90
Route definition and HTTP method handling for creating RESTful APIs and web applications.
91
92
```javascript { .api }
93
/**
94
* HTTP method routing functions
95
*/
96
interface Application {
97
/** Handle GET requests */
98
get(path: string, ...handlers: RequestHandler[]): Application;
99
/** Handle POST requests */
100
post(path: string, ...handlers: RequestHandler[]): Application;
101
/** Handle PUT requests */
102
put(path: string, ...handlers: RequestHandler[]): Application;
103
/** Handle DELETE requests */
104
delete(path: string, ...handlers: RequestHandler[]): Application;
105
/** Handle PATCH requests */
106
patch(path: string, ...handlers: RequestHandler[]): Application;
107
/** Handle all HTTP methods */
108
all(path: string, ...handlers: RequestHandler[]): Application;
109
/** Create route instance for chaining */
110
route(path: string): Route;
111
}
112
113
/**
114
* Request handler function signature
115
*/
116
type RequestHandler = (req: Request, res: Response, next: NextFunction) => void | Promise<void>;
117
```
118
119
[HTTP Routing](./routing.md)
120
121
### Middleware System
122
123
Middleware functions for request processing, authentication, logging, and request/response modification.
124
125
```javascript { .api }
126
/**
127
* Middleware mounting and built-in middleware functions
128
*/
129
interface Application {
130
/** Mount middleware at optional path */
131
use(path?: string, ...middleware: RequestHandler[]): Application;
132
/** Add parameter middleware */
133
param(name: string, handler: ParamHandler): Application;
134
}
135
136
/**
137
* Built-in middleware functions
138
*/
139
declare const express: {
140
/** Parse JSON request bodies */
141
json(options?: JsonOptions): RequestHandler;
142
/** Parse URL-encoded request bodies */
143
urlencoded(options?: UrlencodedOptions): RequestHandler;
144
/** Parse raw request bodies */
145
raw(options?: RawOptions): RequestHandler;
146
/** Parse text request bodies */
147
text(options?: TextOptions): RequestHandler;
148
/** Serve static files */
149
static(root: string, options?: StaticOptions): RequestHandler;
150
};
151
152
type ParamHandler = (req: Request, res: Response, next: NextFunction, value: any, name: string) => void;
153
```
154
155
[Middleware](./middleware.md)
156
157
### Request Processing
158
159
Enhanced request object with Express-specific properties and methods for handling incoming HTTP requests.
160
161
```javascript { .api }
162
/**
163
* Express request object extends Node.js IncomingMessage
164
*/
165
interface Request {
166
/** Reference to Express app instance */
167
app: Application;
168
/** Base URL path */
169
baseUrl: string;
170
/** Request body (populated by body parsers) */
171
body: any;
172
/** Request cookies */
173
cookies: { [key: string]: string };
174
/** Request hostname */
175
hostname: string;
176
/** Remote IP address */
177
ip: string;
178
/** Original request URL */
179
originalUrl: string;
180
/** Route parameters */
181
params: { [key: string]: string };
182
/** Request path */
183
path: string;
184
/** Request protocol */
185
protocol: string;
186
/** Query string parameters */
187
query: { [key: string]: any };
188
/** Currently matched route */
189
route: Route;
190
/** Check if request is secure (HTTPS) */
191
secure: boolean;
192
/** Check if request is XMLHttpRequest */
193
xhr: boolean;
194
}
195
```
196
197
[Request Processing](./request.md)
198
199
### Response Handling
200
201
Enhanced response object with Express-specific methods for sending responses, setting headers, and handling cookies.
202
203
```javascript { .api }
204
/**
205
* Express response object extends Node.js ServerResponse
206
*/
207
interface Response {
208
/** Reference to Express app instance */
209
app: Application;
210
/** Response locals for template rendering */
211
locals: { [key: string]: any };
212
213
/** Send response */
214
send(body?: any): Response;
215
/** Send JSON response */
216
json(obj?: any): Response;
217
/** Send status code */
218
sendStatus(statusCode: number): Response;
219
/** Send file */
220
sendFile(path: string, options?: SendFileOptions, callback?: (err?: Error) => void): void;
221
/** Redirect request */
222
redirect(status: number, url: string): void;
223
redirect(url: string): void;
224
/** Set status code */
225
status(statusCode: number): Response;
226
/** Set response header */
227
set(field: string | { [key: string]: string }, value?: string): Response;
228
/** Set cookie */
229
cookie(name: string, value: string, options?: CookieOptions): Response;
230
/** Clear cookie */
231
clearCookie(name: string, options?: CookieOptions): Response;
232
}
233
```
234
235
[Response Handling](./response.md)
236
237
### Router System
238
239
Modular route handlers for organizing and grouping related routes with middleware support.
240
241
```javascript { .api }
242
/**
243
* Create router instance for modular route handlers
244
* @param options Router configuration options
245
* @returns Router instance
246
*/
247
function Router(options?: RouterOptions): Router;
248
249
/**
250
* Router instance with route handling capabilities
251
*/
252
interface Router {
253
/** Mount middleware */
254
use(path?: string, ...handlers: RequestHandler[]): Router;
255
/** Handle all HTTP methods */
256
all(path: string, ...handlers: RequestHandler[]): Router;
257
/** HTTP method handlers */
258
get(path: string, ...handlers: RequestHandler[]): Router;
259
post(path: string, ...handlers: RequestHandler[]): Router;
260
put(path: string, ...handlers: RequestHandler[]): Router;
261
delete(path: string, ...handlers: RequestHandler[]): Router;
262
patch(path: string, ...handlers: RequestHandler[]): Router;
263
/** Create route for chaining */
264
route(path: string): Route;
265
/** Add parameter middleware */
266
param(name: string, handler: ParamHandler): Router;
267
}
268
269
interface RouterOptions {
270
/** Enable case-sensitive routing */
271
caseSensitive?: boolean;
272
/** Merge params from parent */
273
mergeParams?: boolean;
274
/** Enable strict routing */
275
strict?: boolean;
276
}
277
```
278
279
[Router System](./router.md)
280
281
## Core Types
282
283
```javascript { .api }
284
/**
285
* Next function for middleware chain
286
*/
287
type NextFunction = (err?: Error) => void;
288
289
/**
290
* Route object for individual routes
291
*/
292
interface Route {
293
/** Route path pattern */
294
path: string;
295
/** HTTP methods handled */
296
methods: { [method: string]: boolean };
297
/** Handle all HTTP methods */
298
all(...handlers: RequestHandler[]): Route;
299
/** HTTP method handlers */
300
get(...handlers: RequestHandler[]): Route;
301
post(...handlers: RequestHandler[]): Route;
302
put(...handlers: RequestHandler[]): Route;
303
delete(...handlers: RequestHandler[]): Route;
304
patch(...handlers: RequestHandler[]): Route;
305
}
306
307
/**
308
* Error handling middleware signature
309
*/
310
type ErrorRequestHandler = (err: Error, req: Request, res: Response, next: NextFunction) => void;
311
```