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@5.1.0

index.md docs/

1
# Express
2
3
Express 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.
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
// Middleware
31
app.use(express.json());
32
app.use(express.static('public'));
33
34
// Routing
35
app.get('/', (req, res) => {
36
res.send('Hello World!');
37
});
38
39
app.post('/users', (req, res) => {
40
res.json({ message: 'User created', data: req.body });
41
});
42
43
// Start server
44
app.listen(3000, () => {
45
console.log('Server running on port 3000');
46
});
47
```
48
49
## Architecture
50
51
Express is built around several core concepts:
52
53
- **Application**: The main Express application instance that handles HTTP requests
54
- **Middleware**: Functions that process requests and responses in a pipeline
55
- **Routing**: URL pattern matching and HTTP method handling
56
- **Request/Response Objects**: Enhanced Node.js IncomingMessage and ServerResponse objects
57
- **Template Engine Integration**: Support for various view engines like EJS, Handlebars, Pug
58
59
## Capabilities
60
61
### Application Management
62
63
Core application creation, configuration, and server management functionality. Handles application settings, middleware mounting, and HTTP server lifecycle.
64
65
```javascript { .api }
66
/**
67
* Creates an Express application instance
68
* @returns {Function} Express application function
69
*/
70
function express();
71
72
/**
73
* Application prototype with routing and middleware methods
74
*/
75
interface Application extends Function {
76
// Settings
77
set(setting: string, val: any): this;
78
get(setting: string): any;
79
enable(setting: string): this;
80
disable(setting: string): this;
81
enabled(setting: string): boolean;
82
disabled(setting: string): boolean;
83
84
// Server lifecycle
85
listen(port?: number, hostname?: string, callback?: Function): Server;
86
87
// Middleware and routing
88
use(...handlers: Handler[]): this;
89
use(path: string, ...handlers: Handler[]): this;
90
route(path: string): Route;
91
}
92
93
/**
94
* Express prototypes - accessible for extending functionality
95
*/
96
express.application: Application; // Application prototype
97
express.request: Request; // Request prototype
98
express.response: Response; // Response prototype
99
100
/**
101
* Express constructors
102
*/
103
express.Route: typeof Route; // Route constructor
104
express.Router: typeof Router; // Router constructor
105
```
106
107
[Application Management](./application.md)
108
109
### HTTP Request Processing
110
111
Request object enhancements providing access to headers, query parameters, content negotiation, and request metadata analysis.
112
113
```javascript { .api }
114
/**
115
* Enhanced request object with Express-specific properties and methods
116
*/
117
interface Request extends IncomingMessage {
118
// Header access
119
get(name: string): string | undefined;
120
header(name: string): string | undefined;
121
122
// Content negotiation
123
accepts(types: string | string[]): string | false;
124
acceptsEncodings(encodings: string | string[]): string | false;
125
acceptsCharsets(charsets: string | string[]): string | false;
126
acceptsLanguages(langs: string | string[]): string | false;
127
128
// Request analysis
129
is(type: string | string[]): string | false;
130
131
// Properties
132
query: { [key: string]: any };
133
params: { [key: string]: string };
134
body: any;
135
protocol: string;
136
secure: boolean;
137
ip: string;
138
hostname: string;
139
path: string;
140
fresh: boolean;
141
stale: boolean;
142
xhr: boolean;
143
}
144
```
145
146
[Request Processing](./request.md)
147
148
### HTTP Response Generation
149
150
Response object enhancements for sending various types of responses, setting headers, managing cookies, and handling redirects.
151
152
```javascript { .api }
153
/**
154
* Enhanced response object with Express-specific methods
155
*/
156
interface Response extends ServerResponse {
157
// Response sending
158
send(body?: any): this;
159
json(obj: any): this;
160
jsonp(obj: any): this;
161
sendStatus(statusCode: number): this;
162
sendFile(path: string, options?: object, callback?: Function): void;
163
164
// Headers and status
165
status(code: number): this;
166
set(field: string, val: string | string[]): this;
167
get(field: string): string | undefined;
168
169
// Cookies
170
cookie(name: string, val: any, options?: object): this;
171
clearCookie(name: string, options?: object): this;
172
173
// Redirects
174
redirect(url: string): void;
175
redirect(status: number, url: string): void;
176
}
177
```
178
179
[Response Generation](./response.md)
180
181
### Routing System
182
183
Router and Route classes for organizing application endpoints, handling HTTP methods, and managing route parameters.
184
185
```javascript { .api }
186
/**
187
* Router constructor for creating modular route handlers
188
* @param {object} options - Router configuration options
189
* @returns {Router} Router instance
190
*/
191
function Router(options?: object): Router;
192
193
/**
194
* Route constructor for individual route management
195
* @param {string} path - Route path pattern
196
* @returns {Route} Route instance
197
*/
198
function Route(path: string): Route;
199
```
200
201
[Routing System](./router.md)
202
203
### Built-in Middleware
204
205
Express includes several built-in middleware functions for common web application needs including body parsing and static file serving.
206
207
```javascript { .api }
208
// Body parsing middleware
209
express.json(options?: object): Function;
210
express.urlencoded(options?: object): Function;
211
express.raw(options?: object): Function;
212
express.text(options?: object): Function;
213
214
// Static file serving
215
express.static(root: string, options?: object): Function;
216
```
217
218
[Built-in Middleware](./middleware.md)
219
220
## Types
221
222
```javascript { .api }
223
/**
224
* Generic middleware function signature
225
*/
226
type Handler = (req: Request, res: Response, next: NextFunction) => void;
227
228
/**
229
* Next function for middleware chain continuation
230
*/
231
type NextFunction = (err?: any) => void;
232
233
/**
234
* HTTP Server instance from Node.js
235
*/
236
interface Server {
237
listen(port?: number, hostname?: string, callback?: Function): this;
238
close(callback?: Function): this;
239
}
240
```