appstage 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/dist/bin/build.js +532 -0
- package/dist/bin/startDev.js +545 -0
- package/dist/bin/startProd.js +545 -0
- package/dist/index.cjs +671 -0
- package/dist/index.d.ts +1162 -0
- package/dist/index.mjs +622 -0
- package/index.ts +32 -0
- package/package.json +39 -0
- package/src/controllers/dir.ts +119 -0
- package/src/controllers/unhandledError.ts +15 -0
- package/src/controllers/unhandledRoute.ts +14 -0
- package/src/lib/lang/getEffectiveLocale.ts +52 -0
- package/src/lib/lang/getLocales.ts +10 -0
- package/src/lib/lang/toLanguage.ts +3 -0
- package/src/lib/logger/LogOptions.ts +8 -0
- package/src/lib/logger/ansiEscapeCodes.ts +6 -0
- package/src/lib/logger/levelColors.ts +4 -0
- package/src/lib/logger/log.ts +82 -0
- package/src/middleware/init.ts +22 -0
- package/src/middleware/lang.ts +83 -0
- package/src/middleware/requestEvents.ts +29 -0
- package/src/scripts/bin/build.ts +5 -0
- package/src/scripts/bin/startDev.ts +5 -0
- package/src/scripts/bin/startProd.ts +5 -0
- package/src/scripts/build.ts +45 -0
- package/src/scripts/cli.ts +46 -0
- package/src/scripts/const/commonBuildOptions.ts +13 -0
- package/src/scripts/const/entryExtensions.ts +1 -0
- package/src/scripts/start.ts +18 -0
- package/src/scripts/types/BuildParams.ts +9 -0
- package/src/scripts/utils/buildClient.ts +41 -0
- package/src/scripts/utils/buildServer.ts +35 -0
- package/src/scripts/utils/buildServerCSS.ts +38 -0
- package/src/scripts/utils/createPostbuildPlugins.ts +66 -0
- package/src/scripts/utils/getEntries.ts +22 -0
- package/src/scripts/utils/getEntryPoints.ts +25 -0
- package/src/scripts/utils/getFirstAvailable.ts +22 -0
- package/src/scripts/utils/populateEntries.ts +28 -0
- package/src/scripts/utils/toImportPath.ts +12 -0
- package/src/types/Controller.ts +4 -0
- package/src/types/ErrorController.ts +3 -0
- package/src/types/LogEventPayload.ts +12 -0
- package/src/types/LogLevel.ts +1 -0
- package/src/types/Middleware.ts +7 -0
- package/src/types/MiddlewareSet.ts +3 -0
- package/src/types/RenderStatus.ts +9 -0
- package/src/types/ReqCtx.ts +11 -0
- package/src/types/TransformContent.ts +11 -0
- package/src/types/express.d.ts +15 -0
- package/src/types/global.d.ts +17 -0
- package/src/utils/createApp.ts +44 -0
- package/src/utils/cspNonce.ts +6 -0
- package/src/utils/emitLog.ts +18 -0
- package/src/utils/getEntries.ts +22 -0
- package/src/utils/getStatusMessage.ts +5 -0
- package/src/utils/injectNonce.ts +7 -0
- package/src/utils/renderStatus.ts +20 -0
- package/src/utils/resolveFilePath.ts +78 -0
- package/src/utils/serializeState.ts +3 -0
- package/src/utils/servePipeableStream.ts +32 -0
- package/tsconfig.json +18 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1162 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IncomingMessage } from "node:http";
|
|
3
|
+
import { Application, CookieOptions, ErrorRequestHandler, Request, RequestHandler, Response } from "express";
|
|
4
|
+
import { SendOptions } from "send";
|
|
5
|
+
import { EventEmitter } from "events";
|
|
6
|
+
import * as http from "http";
|
|
7
|
+
import { ParsedQs } from "qs";
|
|
8
|
+
import { Options, Ranges, Result } from "range-parser";
|
|
9
|
+
|
|
10
|
+
/** Defines a final request handler sending a response */
|
|
11
|
+
type Controller<T = void> = (params: T) => RequestHandler;
|
|
12
|
+
|
|
13
|
+
type TransformContent = (req: Request, res: Response, params: {
|
|
14
|
+
content: string;
|
|
15
|
+
path?: string;
|
|
16
|
+
name?: string;
|
|
17
|
+
}) => string | Promise<string>;
|
|
18
|
+
|
|
19
|
+
type ResolveFilePathParams = {
|
|
20
|
+
name: string;
|
|
21
|
+
dir?: string;
|
|
22
|
+
lang?: string;
|
|
23
|
+
supportedLocales?: string[]; /** Allowed file extensions. */
|
|
24
|
+
ext?: string | string[];
|
|
25
|
+
/**
|
|
26
|
+
* Whether an index file should be checked if the resolved file name
|
|
27
|
+
* doesn't correspond to an existing file.
|
|
28
|
+
*
|
|
29
|
+
* @defaultValue `true`
|
|
30
|
+
*/
|
|
31
|
+
index?: boolean;
|
|
32
|
+
};
|
|
33
|
+
declare function resolveFilePath({
|
|
34
|
+
name,
|
|
35
|
+
dir,
|
|
36
|
+
lang,
|
|
37
|
+
supportedLocales,
|
|
38
|
+
ext,
|
|
39
|
+
index
|
|
40
|
+
}: ResolveFilePathParams): Promise<string | undefined>;
|
|
41
|
+
|
|
42
|
+
type ZeroTransform = false | null | undefined;
|
|
43
|
+
type DirParams = Partial<Pick<ResolveFilePathParams, "supportedLocales" | "index">> & {
|
|
44
|
+
/** Directory path to serve files from. */path: string;
|
|
45
|
+
/**
|
|
46
|
+
* File name.
|
|
47
|
+
* By default, the portion of `req.path` after the last slash.
|
|
48
|
+
*/
|
|
49
|
+
name?: string | undefined | ((req: Request, res: Response) => string | undefined);
|
|
50
|
+
/**
|
|
51
|
+
* Allowed file extensions.
|
|
52
|
+
*
|
|
53
|
+
* @defaultValue `['html', 'htm']`
|
|
54
|
+
*/
|
|
55
|
+
ext?: ResolveFilePathParams["ext"];
|
|
56
|
+
/**
|
|
57
|
+
* Custom transforms applied to the file content.
|
|
58
|
+
*
|
|
59
|
+
* Example: Use `injectNonce` from this package to inject the `nonce`
|
|
60
|
+
* value generated for the current request into the `{{nonce}}`
|
|
61
|
+
* placeholders in an HTML file.
|
|
62
|
+
*/
|
|
63
|
+
transform?: TransformContent | ZeroTransform | (TransformContent | ZeroTransform)[];
|
|
64
|
+
supportedLocales?: string[];
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Serves files from the specified directory path in a locale-aware
|
|
68
|
+
* fashion after applying optional transforms.
|
|
69
|
+
*
|
|
70
|
+
* A file ending with `.<lang>.<ext>` is picked first if the `<lang>`
|
|
71
|
+
* part matches `req.ctx.lang`. If the `supportedLocales` array is
|
|
72
|
+
* provided, the `*.<lang>.<ext>` file is picked only if the given
|
|
73
|
+
* array contains `req.ctx.lang`. Otherwise, a file without the locale
|
|
74
|
+
* in its path (`*.<ext>`) is picked.
|
|
75
|
+
*/
|
|
76
|
+
declare const dir$1: Controller<DirParams>;
|
|
77
|
+
|
|
78
|
+
type ErrorController<T = void> = (params: T) => ErrorRequestHandler;
|
|
79
|
+
|
|
80
|
+
declare const unhandledError: ErrorController;
|
|
81
|
+
|
|
82
|
+
declare const unhandledRoute: Controller;
|
|
83
|
+
|
|
84
|
+
declare function getEffectiveLocale(preferredLocales: string[] | undefined, supportedLocales: string[] | undefined): string | undefined;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Parses a language range string (typically a value of the 'Accept-Language'
|
|
88
|
+
* HTTP request header) and returns a corresponding array of locales
|
|
89
|
+
* @example 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5'
|
|
90
|
+
*/
|
|
91
|
+
declare function getLocales(languageRange: string | undefined): string[];
|
|
92
|
+
|
|
93
|
+
declare function toLanguage(locale: string): string;
|
|
94
|
+
|
|
95
|
+
type LogOptions = {
|
|
96
|
+
timestamp?: number;
|
|
97
|
+
level?: "debug" | "info" | "warn" | "error";
|
|
98
|
+
data?: unknown;
|
|
99
|
+
req?: Request;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
declare function log(message?: string | Error | undefined, {
|
|
103
|
+
timestamp,
|
|
104
|
+
level,
|
|
105
|
+
data,
|
|
106
|
+
req
|
|
107
|
+
}?: LogOptions): void;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Defines an intermediate request handler performing manipulations
|
|
111
|
+
* with request data without sending a response
|
|
112
|
+
*/
|
|
113
|
+
type Middleware<T = void> = (params: T) => RequestHandler;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Initializes the request context on `req.ctx`.
|
|
117
|
+
*/
|
|
118
|
+
declare const init: Middleware;
|
|
119
|
+
|
|
120
|
+
type LangParams = {
|
|
121
|
+
supportedLocales?: string[];
|
|
122
|
+
shouldSetCookie?: boolean;
|
|
123
|
+
shouldRedirect?: boolean;
|
|
124
|
+
langCookieOptions?: CookieOptions;
|
|
125
|
+
};
|
|
126
|
+
declare const lang$1: Middleware<LangParams | void>;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Adds event handlers, like logging, to essential request phases.
|
|
130
|
+
*/
|
|
131
|
+
declare const requestEvents: Middleware;
|
|
132
|
+
|
|
133
|
+
type BuildParams = {
|
|
134
|
+
targetDir: string;
|
|
135
|
+
publicAssetsDir: string;
|
|
136
|
+
silent?: boolean;
|
|
137
|
+
watch?: boolean;
|
|
138
|
+
watchClient?: boolean;
|
|
139
|
+
watchServer?: boolean;
|
|
140
|
+
start?: boolean;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
declare function build(params: BuildParams): Promise<void>;
|
|
144
|
+
|
|
145
|
+
declare function cli(args?: string[]): Promise<void>;
|
|
146
|
+
|
|
147
|
+
declare function start(nodeEnv?: string, host?: string): Promise<void>;
|
|
148
|
+
|
|
149
|
+
type LogLevel = "error" | "debug" | "info" | "warn";
|
|
150
|
+
|
|
151
|
+
type LogEventPayload = {
|
|
152
|
+
timestamp?: number;
|
|
153
|
+
level?: LogLevel;
|
|
154
|
+
message?: string | Error;
|
|
155
|
+
status?: number;
|
|
156
|
+
req?: Request;
|
|
157
|
+
res?: Response;
|
|
158
|
+
data?: unknown;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
type MiddlewareSet<T = void> = (params: T) => RequestHandler[];
|
|
162
|
+
|
|
163
|
+
type SendParam = Parameters<Response["send"]>[0];
|
|
164
|
+
type RenderStatus = (req: Request, res: Response, payload?: unknown) => Promise<SendParam>;
|
|
165
|
+
|
|
166
|
+
/** Session request data collected on the server via `req.ctx` */
|
|
167
|
+
type ReqCtx = {
|
|
168
|
+
/** Request ID */id?: string; /** CSP nonce */
|
|
169
|
+
nonce?: string; /** When the request started to be handled */
|
|
170
|
+
startTime?: number; /** User locale */
|
|
171
|
+
lang?: string;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
declare global {
|
|
175
|
+
namespace Express {
|
|
176
|
+
// These open interfaces may be extended in an application-specific manner via declaration merging.
|
|
177
|
+
// See for example method-override.d.ts (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/method-override/index.d.ts)
|
|
178
|
+
interface Request {}
|
|
179
|
+
interface Response {}
|
|
180
|
+
interface Locals {}
|
|
181
|
+
interface Application {}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
interface NextFunction {
|
|
185
|
+
(err?: any): void;
|
|
186
|
+
/**
|
|
187
|
+
* "Break-out" of a router by calling {next('router')};
|
|
188
|
+
* @see {https://expressjs.com/en/guide/using-middleware.html#middleware.router}
|
|
189
|
+
*/
|
|
190
|
+
(deferToNext: "router"): void;
|
|
191
|
+
/**
|
|
192
|
+
* "Break-out" of a route by calling {next('route')};
|
|
193
|
+
* @see {https://expressjs.com/en/guide/using-middleware.html#middleware.application}
|
|
194
|
+
*/
|
|
195
|
+
(deferToNext: "route"): void;
|
|
196
|
+
}
|
|
197
|
+
interface ParamsDictionary {
|
|
198
|
+
[key: string]: string | string[];
|
|
199
|
+
[key: number]: string;
|
|
200
|
+
}
|
|
201
|
+
interface ParamsFlatDictionary {
|
|
202
|
+
[key: string | number]: string;
|
|
203
|
+
}
|
|
204
|
+
interface Locals extends Express.Locals {}
|
|
205
|
+
interface RequestHandler$1<P = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>> {
|
|
206
|
+
// tslint:disable-next-line callable-types (This is extended from and can't extend from a type alias in ts<2.2)
|
|
207
|
+
(req: Request$1<P, ResBody, ReqBody, ReqQuery, LocalsObj>, res: Response$1<ResBody, LocalsObj>, next: NextFunction): unknown;
|
|
208
|
+
}
|
|
209
|
+
type ErrorRequestHandler$1<P = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>> = (err: any, req: Request$1<P, ResBody, ReqBody, ReqQuery, LocalsObj>, res: Response$1<ResBody, LocalsObj>, next: NextFunction) => unknown;
|
|
210
|
+
type PathParams = string | RegExp | Array<string | RegExp>;
|
|
211
|
+
type RequestHandlerParams<P = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>> = RequestHandler$1<P, ResBody, ReqBody, ReqQuery, LocalsObj> | ErrorRequestHandler$1<P, ResBody, ReqBody, ReqQuery, LocalsObj> | Array<RequestHandler$1<P> | ErrorRequestHandler$1<P>>;
|
|
212
|
+
type RemoveTail<S extends string, Tail extends string> = S extends `${infer P}${Tail}` ? P : S;
|
|
213
|
+
type GetRouteParameter<S extends string> = RemoveTail<RemoveTail<RemoveTail<S, `/${string}`>, `-${string}`>, `.${string}`>; // dprint-ignore
|
|
214
|
+
type RouteParameters<Route extends string | RegExp> = Route extends string ? Route extends `${infer Required}{${infer Optional}}${infer Next}` ? ParseRouteParameters<Required> & Partial<ParseRouteParameters<Optional>> & RouteParameters<Next> : ParseRouteParameters<Route> : ParamsFlatDictionary;
|
|
215
|
+
type ParseRouteParameters<Route extends string> = string extends Route ? ParamsDictionary : Route extends `${string}:${infer Rest}` ? (GetRouteParameter<Rest> extends never ? ParamsDictionary : { [P in GetRouteParameter<Rest>]: string }) & (Rest extends `${GetRouteParameter<Rest>}${infer Next}` ? RouteParameters<Next> : unknown) : Route extends `${string}*${infer Rest}` ? (GetRouteParameter<Rest> extends never ? ParamsDictionary : { [P in GetRouteParameter<Rest>]: string[] }) & (Rest extends `${GetRouteParameter<Rest>}${infer Next}` ? RouteParameters<Next> : unknown) : {};
|
|
216
|
+
/* eslint-disable @definitelytyped/no-unnecessary-generics */
|
|
217
|
+
interface IRouterMatcher<T, Method extends "all" | "get" | "post" | "put" | "delete" | "patch" | "options" | "head" = any> {
|
|
218
|
+
<Route extends string | RegExp, P = RouteParameters<Route>, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>>(// (it's used as the default type parameter for P)
|
|
219
|
+
path: Route, // (This generic is meant to be passed explicitly.)
|
|
220
|
+
...handlers: Array<RequestHandler$1<P, ResBody, ReqBody, ReqQuery, LocalsObj>>): T;
|
|
221
|
+
<Path extends string | RegExp, P = RouteParameters<Path>, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>>(// (it's used as the default type parameter for P)
|
|
222
|
+
path: Path, // (This generic is meant to be passed explicitly.)
|
|
223
|
+
...handlers: Array<RequestHandlerParams<P, ResBody, ReqBody, ReqQuery, LocalsObj>>): T;
|
|
224
|
+
<P = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>>(path: PathParams, // (This generic is meant to be passed explicitly.)
|
|
225
|
+
...handlers: Array<RequestHandler$1<P, ResBody, ReqBody, ReqQuery, LocalsObj>>): T;
|
|
226
|
+
<P = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>>(path: PathParams, // (This generic is meant to be passed explicitly.)
|
|
227
|
+
...handlers: Array<RequestHandlerParams<P, ResBody, ReqBody, ReqQuery, LocalsObj>>): T;
|
|
228
|
+
(path: PathParams, subApplication: Application$1): T;
|
|
229
|
+
}
|
|
230
|
+
interface IRouterHandler<T, Route extends string | RegExp = string> {
|
|
231
|
+
(...handlers: Array<RequestHandler$1<RouteParameters<Route>>>): T;
|
|
232
|
+
(...handlers: Array<RequestHandlerParams<RouteParameters<Route>>>): T;
|
|
233
|
+
<P = RouteParameters<Route>, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>>(// (This generic is meant to be passed explicitly.)
|
|
234
|
+
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
|
|
235
|
+
...handlers: Array<RequestHandler$1<P, ResBody, ReqBody, ReqQuery, LocalsObj>>): T;
|
|
236
|
+
<P = RouteParameters<Route>, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>>(// (This generic is meant to be passed explicitly.)
|
|
237
|
+
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
|
|
238
|
+
...handlers: Array<RequestHandlerParams<P, ResBody, ReqBody, ReqQuery, LocalsObj>>): T;
|
|
239
|
+
<P = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>>(// (This generic is meant to be passed explicitly.)
|
|
240
|
+
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
|
|
241
|
+
...handlers: Array<RequestHandler$1<P, ResBody, ReqBody, ReqQuery, LocalsObj>>): T;
|
|
242
|
+
<P = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>>(// (This generic is meant to be passed explicitly.)
|
|
243
|
+
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
|
|
244
|
+
...handlers: Array<RequestHandlerParams<P, ResBody, ReqBody, ReqQuery, LocalsObj>>): T;
|
|
245
|
+
}
|
|
246
|
+
/* eslint-enable @definitelytyped/no-unnecessary-generics */
|
|
247
|
+
interface IRouter extends RequestHandler$1 {
|
|
248
|
+
/**
|
|
249
|
+
* Map the given param placeholder `name`(s) to the given callback(s).
|
|
250
|
+
*
|
|
251
|
+
* Parameter mapping is used to provide pre-conditions to routes
|
|
252
|
+
* which use normalized placeholders. For example a _:user_id_ parameter
|
|
253
|
+
* could automatically load a user's information from the database without
|
|
254
|
+
* any additional code,
|
|
255
|
+
*
|
|
256
|
+
* The callback uses the samesignature as middleware, the only differencing
|
|
257
|
+
* being that the value of the placeholder is passed, in this case the _id_
|
|
258
|
+
* of the user. Once the `next()` function is invoked, just like middleware
|
|
259
|
+
* it will continue on to execute the route, or subsequent parameter functions.
|
|
260
|
+
*
|
|
261
|
+
* app.param('user_id', function(req, res, next, id){
|
|
262
|
+
* User.find(id, function(err, user){
|
|
263
|
+
* if (err) {
|
|
264
|
+
* next(err);
|
|
265
|
+
* } else if (user) {
|
|
266
|
+
* req.user = user;
|
|
267
|
+
* next();
|
|
268
|
+
* } else {
|
|
269
|
+
* next(new Error('failed to load user'));
|
|
270
|
+
* }
|
|
271
|
+
* });
|
|
272
|
+
* });
|
|
273
|
+
*/
|
|
274
|
+
param(name: string, handler: RequestParamHandler): this;
|
|
275
|
+
/**
|
|
276
|
+
* Special-cased "all" method, applying the given route `path`,
|
|
277
|
+
* middleware, and callback to _every_ HTTP method.
|
|
278
|
+
*/
|
|
279
|
+
all: IRouterMatcher<this, "all">;
|
|
280
|
+
get: IRouterMatcher<this, "get">;
|
|
281
|
+
post: IRouterMatcher<this, "post">;
|
|
282
|
+
put: IRouterMatcher<this, "put">;
|
|
283
|
+
delete: IRouterMatcher<this, "delete">;
|
|
284
|
+
patch: IRouterMatcher<this, "patch">;
|
|
285
|
+
options: IRouterMatcher<this, "options">;
|
|
286
|
+
head: IRouterMatcher<this, "head">;
|
|
287
|
+
checkout: IRouterMatcher<this>;
|
|
288
|
+
connect: IRouterMatcher<this>;
|
|
289
|
+
copy: IRouterMatcher<this>;
|
|
290
|
+
lock: IRouterMatcher<this>;
|
|
291
|
+
merge: IRouterMatcher<this>;
|
|
292
|
+
mkactivity: IRouterMatcher<this>;
|
|
293
|
+
mkcol: IRouterMatcher<this>;
|
|
294
|
+
move: IRouterMatcher<this>;
|
|
295
|
+
"m-search": IRouterMatcher<this>;
|
|
296
|
+
notify: IRouterMatcher<this>;
|
|
297
|
+
propfind: IRouterMatcher<this>;
|
|
298
|
+
proppatch: IRouterMatcher<this>;
|
|
299
|
+
purge: IRouterMatcher<this>;
|
|
300
|
+
report: IRouterMatcher<this>;
|
|
301
|
+
search: IRouterMatcher<this>;
|
|
302
|
+
subscribe: IRouterMatcher<this>;
|
|
303
|
+
trace: IRouterMatcher<this>;
|
|
304
|
+
unlock: IRouterMatcher<this>;
|
|
305
|
+
unsubscribe: IRouterMatcher<this>;
|
|
306
|
+
link: IRouterMatcher<this>;
|
|
307
|
+
unlink: IRouterMatcher<this>;
|
|
308
|
+
use: IRouterHandler<this> & IRouterMatcher<this>;
|
|
309
|
+
route<T extends string | RegExp>(prefix: T): IRoute<T>;
|
|
310
|
+
route(prefix: PathParams): IRoute;
|
|
311
|
+
/**
|
|
312
|
+
* Stack of configured routes
|
|
313
|
+
*/
|
|
314
|
+
stack: ILayer[];
|
|
315
|
+
}
|
|
316
|
+
interface ILayer {
|
|
317
|
+
route?: IRoute;
|
|
318
|
+
name: string | "<anonymous>";
|
|
319
|
+
params?: Record<string, any>;
|
|
320
|
+
keys: string[];
|
|
321
|
+
path?: string;
|
|
322
|
+
method: string;
|
|
323
|
+
regexp: RegExp;
|
|
324
|
+
handle: (req: Request$1, res: Response$1, next: NextFunction) => any;
|
|
325
|
+
}
|
|
326
|
+
interface IRoute<Route extends string | RegExp = string> {
|
|
327
|
+
path: string;
|
|
328
|
+
stack: ILayer[];
|
|
329
|
+
all: IRouterHandler<this, Route>;
|
|
330
|
+
get: IRouterHandler<this, Route>;
|
|
331
|
+
post: IRouterHandler<this, Route>;
|
|
332
|
+
put: IRouterHandler<this, Route>;
|
|
333
|
+
delete: IRouterHandler<this, Route>;
|
|
334
|
+
patch: IRouterHandler<this, Route>;
|
|
335
|
+
options: IRouterHandler<this, Route>;
|
|
336
|
+
head: IRouterHandler<this, Route>;
|
|
337
|
+
checkout: IRouterHandler<this, Route>;
|
|
338
|
+
copy: IRouterHandler<this, Route>;
|
|
339
|
+
lock: IRouterHandler<this, Route>;
|
|
340
|
+
merge: IRouterHandler<this, Route>;
|
|
341
|
+
mkactivity: IRouterHandler<this, Route>;
|
|
342
|
+
mkcol: IRouterHandler<this, Route>;
|
|
343
|
+
move: IRouterHandler<this, Route>;
|
|
344
|
+
"m-search": IRouterHandler<this, Route>;
|
|
345
|
+
notify: IRouterHandler<this, Route>;
|
|
346
|
+
purge: IRouterHandler<this, Route>;
|
|
347
|
+
report: IRouterHandler<this, Route>;
|
|
348
|
+
search: IRouterHandler<this, Route>;
|
|
349
|
+
subscribe: IRouterHandler<this, Route>;
|
|
350
|
+
trace: IRouterHandler<this, Route>;
|
|
351
|
+
unlock: IRouterHandler<this, Route>;
|
|
352
|
+
unsubscribe: IRouterHandler<this, Route>;
|
|
353
|
+
}
|
|
354
|
+
interface Router extends IRouter {}
|
|
355
|
+
/**
|
|
356
|
+
* Options passed down into `res.cookie`
|
|
357
|
+
* @link https://expressjs.com/en/api.html#res.cookie
|
|
358
|
+
*/
|
|
359
|
+
interface CookieOptions$1 {
|
|
360
|
+
/** Convenient option for setting the expiry time relative to the current time in **milliseconds**. */
|
|
361
|
+
maxAge?: number | undefined;
|
|
362
|
+
/** Indicates if the cookie should be signed. */
|
|
363
|
+
signed?: boolean | undefined;
|
|
364
|
+
/** Expiry date of the cookie in GMT. If not specified (undefined), creates a session cookie. */
|
|
365
|
+
expires?: Date | undefined;
|
|
366
|
+
/** Flags the cookie to be accessible only by the web server. */
|
|
367
|
+
httpOnly?: boolean | undefined;
|
|
368
|
+
/** Path for the cookie. Defaults to “/”. */
|
|
369
|
+
path?: string | undefined;
|
|
370
|
+
/** Domain name for the cookie. Defaults to the domain name of the app. */
|
|
371
|
+
domain?: string | undefined;
|
|
372
|
+
/** Marks the cookie to be used with HTTPS only. */
|
|
373
|
+
secure?: boolean | undefined;
|
|
374
|
+
/** A synchronous function used for cookie value encoding. Defaults to encodeURIComponent. */
|
|
375
|
+
encode?: ((val: string) => string) | undefined;
|
|
376
|
+
/**
|
|
377
|
+
* Value of the “SameSite” Set-Cookie attribute.
|
|
378
|
+
* @link https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-4.1.1.
|
|
379
|
+
*/
|
|
380
|
+
sameSite?: boolean | "lax" | "strict" | "none" | undefined;
|
|
381
|
+
/**
|
|
382
|
+
* Value of the “Priority” Set-Cookie attribute.
|
|
383
|
+
* @link https://datatracker.ietf.org/doc/html/draft-west-cookie-priority-00#section-4.3
|
|
384
|
+
*/
|
|
385
|
+
priority?: "low" | "medium" | "high";
|
|
386
|
+
/** Marks the cookie to use partioned storage. */
|
|
387
|
+
partitioned?: boolean | undefined;
|
|
388
|
+
}
|
|
389
|
+
type Errback = (err: Error) => void;
|
|
390
|
+
/**
|
|
391
|
+
* @param P For most requests, this should be `ParamsDictionary`, but if you're
|
|
392
|
+
* using this in a route handler for a route that uses a `RegExp`, then `req.params`
|
|
393
|
+
* will only contains strings, in which case you should use `ParamsFlatDictionary` instead.
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
396
|
+
* app.get('/user/:id', (req, res) => res.send(req.params.id)); // implicitly `ParamsDictionary`, parameter is string
|
|
397
|
+
* app.get('/user/*id', (req, res) => res.send(req.params.id)); // implicitly `ParamsDictionary`, parameter is string[]
|
|
398
|
+
* app.get(/user\/(?<id>.*)/, (req, res) => res.send(req.params.id)); // implicitly `ParamsFlatDictionary`, parameter is string
|
|
399
|
+
* app.get(/user\/(.*)/, (req, res) => res.send(req.params[0])); // implicitly `ParamsFlatDictionary`, parameter is string
|
|
400
|
+
*/
|
|
401
|
+
interface Request$1<P = ParamsDictionary, ResBody = any, ReqBody = any, ReqQuery = ParsedQs, LocalsObj extends Record<string, any> = Record<string, any>> extends http.IncomingMessage, Express.Request {
|
|
402
|
+
/**
|
|
403
|
+
* Return request header.
|
|
404
|
+
*
|
|
405
|
+
* The `Referrer` header field is special-cased,
|
|
406
|
+
* both `Referrer` and `Referer` are interchangeable.
|
|
407
|
+
*
|
|
408
|
+
* Examples:
|
|
409
|
+
*
|
|
410
|
+
* req.get('Content-Type');
|
|
411
|
+
* // => "text/plain"
|
|
412
|
+
*
|
|
413
|
+
* req.get('content-type');
|
|
414
|
+
* // => "text/plain"
|
|
415
|
+
*
|
|
416
|
+
* req.get('Something');
|
|
417
|
+
* // => undefined
|
|
418
|
+
*
|
|
419
|
+
* Aliased as `req.header()`.
|
|
420
|
+
*/
|
|
421
|
+
get(name: "set-cookie"): string[] | undefined;
|
|
422
|
+
get(name: string): string | undefined;
|
|
423
|
+
header(name: "set-cookie"): string[] | undefined;
|
|
424
|
+
header(name: string): string | undefined;
|
|
425
|
+
/**
|
|
426
|
+
* Check if the given `type(s)` is acceptable, returning
|
|
427
|
+
* the best match when true, otherwise `undefined`, in which
|
|
428
|
+
* case you should respond with 406 "Not Acceptable".
|
|
429
|
+
*
|
|
430
|
+
* The `type` value may be a single mime type string
|
|
431
|
+
* such as "application/json", the extension name
|
|
432
|
+
* such as "json", a comma-delimted list such as "json, html, text/plain",
|
|
433
|
+
* or an array `["json", "html", "text/plain"]`. When a list
|
|
434
|
+
* or array is given the _best_ match, if any is returned.
|
|
435
|
+
*
|
|
436
|
+
* Examples:
|
|
437
|
+
*
|
|
438
|
+
* // Accept: text/html
|
|
439
|
+
* req.accepts('html');
|
|
440
|
+
* // => "html"
|
|
441
|
+
*
|
|
442
|
+
* // Accept: text/*, application/json
|
|
443
|
+
* req.accepts('html');
|
|
444
|
+
* // => "html"
|
|
445
|
+
* req.accepts('text/html');
|
|
446
|
+
* // => "text/html"
|
|
447
|
+
* req.accepts('json, text');
|
|
448
|
+
* // => "json"
|
|
449
|
+
* req.accepts('application/json');
|
|
450
|
+
* // => "application/json"
|
|
451
|
+
*
|
|
452
|
+
* // Accept: text/*, application/json
|
|
453
|
+
* req.accepts('image/png');
|
|
454
|
+
* req.accepts('png');
|
|
455
|
+
* // => false
|
|
456
|
+
*
|
|
457
|
+
* // Accept: text/*;q=.5, application/json
|
|
458
|
+
* req.accepts(['html', 'json']);
|
|
459
|
+
* req.accepts('html, json');
|
|
460
|
+
* // => "json"
|
|
461
|
+
*/
|
|
462
|
+
accepts(): string[];
|
|
463
|
+
accepts(type: string): string | false;
|
|
464
|
+
accepts(type: string[]): string | false;
|
|
465
|
+
accepts(...type: string[]): string | false;
|
|
466
|
+
/**
|
|
467
|
+
* Returns the first accepted charset of the specified character sets,
|
|
468
|
+
* based on the request's Accept-Charset HTTP header field.
|
|
469
|
+
* If none of the specified charsets is accepted, returns false.
|
|
470
|
+
*
|
|
471
|
+
* For more information, or if you have issues or concerns, see accepts.
|
|
472
|
+
*/
|
|
473
|
+
acceptsCharsets(): string[];
|
|
474
|
+
acceptsCharsets(charset: string): string | false;
|
|
475
|
+
acceptsCharsets(charset: string[]): string | false;
|
|
476
|
+
acceptsCharsets(...charset: string[]): string | false;
|
|
477
|
+
/**
|
|
478
|
+
* Returns the first accepted encoding of the specified encodings,
|
|
479
|
+
* based on the request's Accept-Encoding HTTP header field.
|
|
480
|
+
* If none of the specified encodings is accepted, returns false.
|
|
481
|
+
*
|
|
482
|
+
* For more information, or if you have issues or concerns, see accepts.
|
|
483
|
+
*/
|
|
484
|
+
acceptsEncodings(): string[];
|
|
485
|
+
acceptsEncodings(encoding: string): string | false;
|
|
486
|
+
acceptsEncodings(encoding: string[]): string | false;
|
|
487
|
+
acceptsEncodings(...encoding: string[]): string | false;
|
|
488
|
+
/**
|
|
489
|
+
* Returns the first accepted language of the specified languages,
|
|
490
|
+
* based on the request's Accept-Language HTTP header field.
|
|
491
|
+
* If none of the specified languages is accepted, returns false.
|
|
492
|
+
*
|
|
493
|
+
* For more information, or if you have issues or concerns, see accepts.
|
|
494
|
+
*/
|
|
495
|
+
acceptsLanguages(): string[];
|
|
496
|
+
acceptsLanguages(lang: string): string | false;
|
|
497
|
+
acceptsLanguages(lang: string[]): string | false;
|
|
498
|
+
acceptsLanguages(...lang: string[]): string | false;
|
|
499
|
+
/**
|
|
500
|
+
* Parse Range header field, capping to the given `size`.
|
|
501
|
+
*
|
|
502
|
+
* Unspecified ranges such as "0-" require knowledge of your resource length. In
|
|
503
|
+
* the case of a byte range this is of course the total number of bytes.
|
|
504
|
+
* If the Range header field is not given `undefined` is returned.
|
|
505
|
+
* If the Range header field is given, return value is a result of range-parser.
|
|
506
|
+
* See more ./types/range-parser/index.d.ts
|
|
507
|
+
*
|
|
508
|
+
* NOTE: remember that ranges are inclusive, so for example "Range: users=0-3"
|
|
509
|
+
* should respond with 4 users when available, not 3.
|
|
510
|
+
*/
|
|
511
|
+
range(size: number, options?: Options): Ranges | Result | undefined;
|
|
512
|
+
/**
|
|
513
|
+
* Return an array of Accepted media types
|
|
514
|
+
* ordered from highest quality to lowest.
|
|
515
|
+
*/
|
|
516
|
+
accepted: MediaType[];
|
|
517
|
+
/**
|
|
518
|
+
* Check if the incoming request contains the "Content-Type"
|
|
519
|
+
* header field, and it contains the give mime `type`.
|
|
520
|
+
*
|
|
521
|
+
* Examples:
|
|
522
|
+
*
|
|
523
|
+
* // With Content-Type: text/html; charset=utf-8
|
|
524
|
+
* req.is('html');
|
|
525
|
+
* req.is('text/html');
|
|
526
|
+
* req.is('text/*');
|
|
527
|
+
* // => true
|
|
528
|
+
*
|
|
529
|
+
* // When Content-Type is application/json
|
|
530
|
+
* req.is('json');
|
|
531
|
+
* req.is('application/json');
|
|
532
|
+
* req.is('application/*');
|
|
533
|
+
* // => true
|
|
534
|
+
*
|
|
535
|
+
* req.is('html');
|
|
536
|
+
* // => false
|
|
537
|
+
*/
|
|
538
|
+
is(type: string | string[]): string | false | null;
|
|
539
|
+
/**
|
|
540
|
+
* Return the protocol string "http" or "https"
|
|
541
|
+
* when requested with TLS. When the "trust proxy"
|
|
542
|
+
* setting is enabled the "X-Forwarded-Proto" header
|
|
543
|
+
* field will be trusted. If you're running behind
|
|
544
|
+
* a reverse proxy that supplies https for you this
|
|
545
|
+
* may be enabled.
|
|
546
|
+
*/
|
|
547
|
+
readonly protocol: string;
|
|
548
|
+
/**
|
|
549
|
+
* Short-hand for:
|
|
550
|
+
*
|
|
551
|
+
* req.protocol == 'https'
|
|
552
|
+
*/
|
|
553
|
+
readonly secure: boolean;
|
|
554
|
+
/**
|
|
555
|
+
* Return the remote address, or when
|
|
556
|
+
* "trust proxy" is `true` return
|
|
557
|
+
* the upstream addr.
|
|
558
|
+
*
|
|
559
|
+
* Value may be undefined if the `req.socket` is destroyed
|
|
560
|
+
* (for example, if the client disconnected).
|
|
561
|
+
*/
|
|
562
|
+
readonly ip: string | undefined;
|
|
563
|
+
/**
|
|
564
|
+
* When "trust proxy" is `true`, parse
|
|
565
|
+
* the "X-Forwarded-For" ip address list.
|
|
566
|
+
*
|
|
567
|
+
* For example if the value were "client, proxy1, proxy2"
|
|
568
|
+
* you would receive the array `["client", "proxy1", "proxy2"]`
|
|
569
|
+
* where "proxy2" is the furthest down-stream.
|
|
570
|
+
*/
|
|
571
|
+
readonly ips: string[];
|
|
572
|
+
/**
|
|
573
|
+
* Return subdomains as an array.
|
|
574
|
+
*
|
|
575
|
+
* Subdomains are the dot-separated parts of the host before the main domain of
|
|
576
|
+
* the app. By default, the domain of the app is assumed to be the last two
|
|
577
|
+
* parts of the host. This can be changed by setting "subdomain offset".
|
|
578
|
+
*
|
|
579
|
+
* For example, if the domain is "tobi.ferrets.example.com":
|
|
580
|
+
* If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.
|
|
581
|
+
* If "subdomain offset" is 3, req.subdomains is `["tobi"]`.
|
|
582
|
+
*/
|
|
583
|
+
readonly subdomains: string[];
|
|
584
|
+
/**
|
|
585
|
+
* Short-hand for `url.parse(req.url).pathname`.
|
|
586
|
+
*/
|
|
587
|
+
readonly path: string;
|
|
588
|
+
/**
|
|
589
|
+
* Contains the hostname derived from the `Host` HTTP header.
|
|
590
|
+
*/
|
|
591
|
+
readonly hostname: string;
|
|
592
|
+
/**
|
|
593
|
+
* Contains the host derived from the `Host` HTTP header.
|
|
594
|
+
*/
|
|
595
|
+
readonly host: string;
|
|
596
|
+
/**
|
|
597
|
+
* Check if the request is fresh, aka
|
|
598
|
+
* Last-Modified and/or the ETag
|
|
599
|
+
* still match.
|
|
600
|
+
*/
|
|
601
|
+
readonly fresh: boolean;
|
|
602
|
+
/**
|
|
603
|
+
* Check if the request is stale, aka
|
|
604
|
+
* "Last-Modified" and / or the "ETag" for the
|
|
605
|
+
* resource has changed.
|
|
606
|
+
*/
|
|
607
|
+
readonly stale: boolean;
|
|
608
|
+
/**
|
|
609
|
+
* Check if the request was an _XMLHttpRequest_.
|
|
610
|
+
*/
|
|
611
|
+
readonly xhr: boolean; // body: { username: string; password: string; remember: boolean; title: string; };
|
|
612
|
+
body: ReqBody; // cookies: { string; remember: boolean; };
|
|
613
|
+
cookies: any;
|
|
614
|
+
method: string;
|
|
615
|
+
params: P;
|
|
616
|
+
query: ReqQuery;
|
|
617
|
+
route: any;
|
|
618
|
+
signedCookies: any;
|
|
619
|
+
originalUrl: string;
|
|
620
|
+
url: string;
|
|
621
|
+
baseUrl: string;
|
|
622
|
+
app: Application$1;
|
|
623
|
+
/**
|
|
624
|
+
* After middleware.init executed, Request will contain res and next properties
|
|
625
|
+
* See: express/lib/middleware/init.js
|
|
626
|
+
*/
|
|
627
|
+
res?: Response$1<ResBody, LocalsObj> | undefined;
|
|
628
|
+
next?: NextFunction | undefined;
|
|
629
|
+
}
|
|
630
|
+
interface MediaType {
|
|
631
|
+
value: string;
|
|
632
|
+
quality: number;
|
|
633
|
+
type: string;
|
|
634
|
+
subtype: string;
|
|
635
|
+
}
|
|
636
|
+
type Send<ResBody = any, T = Response$1<ResBody>> = (body?: ResBody) => T;
|
|
637
|
+
interface SendFileOptions extends SendOptions {
|
|
638
|
+
/** Object containing HTTP headers to serve with the file. */
|
|
639
|
+
headers?: Record<string, unknown>;
|
|
640
|
+
}
|
|
641
|
+
interface DownloadOptions extends SendOptions {
|
|
642
|
+
/** Object containing HTTP headers to serve with the file. The header `Content-Disposition` will be overridden by the filename argument. */
|
|
643
|
+
headers?: Record<string, unknown>;
|
|
644
|
+
}
|
|
645
|
+
interface Response$1<ResBody = any, LocalsObj extends Record<string, any> = Record<string, any>, StatusCode extends number = number> extends http.ServerResponse, Express.Response {
|
|
646
|
+
/**
|
|
647
|
+
* Set status `code`.
|
|
648
|
+
*/
|
|
649
|
+
status(code: StatusCode): this;
|
|
650
|
+
/**
|
|
651
|
+
* Set the response HTTP status code to `statusCode` and send its string representation as the response body.
|
|
652
|
+
* @link http://expressjs.com/4x/api.html#res.sendStatus
|
|
653
|
+
*
|
|
654
|
+
* Examples:
|
|
655
|
+
*
|
|
656
|
+
* res.sendStatus(200); // equivalent to res.status(200).send('OK')
|
|
657
|
+
* res.sendStatus(403); // equivalent to res.status(403).send('Forbidden')
|
|
658
|
+
* res.sendStatus(404); // equivalent to res.status(404).send('Not Found')
|
|
659
|
+
* res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error')
|
|
660
|
+
*/
|
|
661
|
+
sendStatus(code: StatusCode): this;
|
|
662
|
+
/**
|
|
663
|
+
* Set Link header field with the given `links`.
|
|
664
|
+
*
|
|
665
|
+
* Examples:
|
|
666
|
+
*
|
|
667
|
+
* res.links({
|
|
668
|
+
* next: 'http://api.example.com/users?page=2',
|
|
669
|
+
* last: 'http://api.example.com/users?page=5'
|
|
670
|
+
* });
|
|
671
|
+
*/
|
|
672
|
+
links(links: any): this;
|
|
673
|
+
/**
|
|
674
|
+
* Send a response.
|
|
675
|
+
*
|
|
676
|
+
* Examples:
|
|
677
|
+
*
|
|
678
|
+
* res.send(new Buffer('wahoo'));
|
|
679
|
+
* res.send({ some: 'json' });
|
|
680
|
+
* res.send('<p>some html</p>');
|
|
681
|
+
* res.status(404).send('Sorry, cant find that');
|
|
682
|
+
*/
|
|
683
|
+
send: Send<ResBody, this>;
|
|
684
|
+
/**
|
|
685
|
+
* Send JSON response.
|
|
686
|
+
*
|
|
687
|
+
* Examples:
|
|
688
|
+
*
|
|
689
|
+
* res.json(null);
|
|
690
|
+
* res.json({ user: 'tj' });
|
|
691
|
+
* res.status(500).json('oh noes!');
|
|
692
|
+
* res.status(404).json('I dont have that');
|
|
693
|
+
*/
|
|
694
|
+
json: Send<ResBody, this>;
|
|
695
|
+
/**
|
|
696
|
+
* Send JSON response with JSONP callback support.
|
|
697
|
+
*
|
|
698
|
+
* Examples:
|
|
699
|
+
*
|
|
700
|
+
* res.jsonp(null);
|
|
701
|
+
* res.jsonp({ user: 'tj' });
|
|
702
|
+
* res.status(500).jsonp('oh noes!');
|
|
703
|
+
* res.status(404).jsonp('I dont have that');
|
|
704
|
+
*/
|
|
705
|
+
jsonp: Send<ResBody, this>;
|
|
706
|
+
/**
|
|
707
|
+
* Transfer the file at the given `path`.
|
|
708
|
+
*
|
|
709
|
+
* Automatically sets the _Content-Type_ response header field.
|
|
710
|
+
* The callback `fn(err)` is invoked when the transfer is complete
|
|
711
|
+
* or when an error occurs. Be sure to check `res.headersSent`
|
|
712
|
+
* if you wish to attempt responding, as the header and some data
|
|
713
|
+
* may have already been transferred.
|
|
714
|
+
*
|
|
715
|
+
* Options:
|
|
716
|
+
*
|
|
717
|
+
* - `maxAge` defaulting to 0 (can be string converted by `ms`)
|
|
718
|
+
* - `root` root directory for relative filenames
|
|
719
|
+
* - `headers` object of headers to serve with file
|
|
720
|
+
* - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them
|
|
721
|
+
*
|
|
722
|
+
* Other options are passed along to `send`.
|
|
723
|
+
*
|
|
724
|
+
* Examples:
|
|
725
|
+
*
|
|
726
|
+
* The following example illustrates how `res.sendFile()` may
|
|
727
|
+
* be used as an alternative for the `static()` middleware for
|
|
728
|
+
* dynamic situations. The code backing `res.sendFile()` is actually
|
|
729
|
+
* the same code, so HTTP cache support etc is identical.
|
|
730
|
+
*
|
|
731
|
+
* app.get('/user/:uid/photos/:file', function(req, res){
|
|
732
|
+
* var uid = req.params.uid
|
|
733
|
+
* , file = req.params.file;
|
|
734
|
+
*
|
|
735
|
+
* req.user.mayViewFilesFrom(uid, function(yes){
|
|
736
|
+
* if (yes) {
|
|
737
|
+
* res.sendFile('/uploads/' + uid + '/' + file);
|
|
738
|
+
* } else {
|
|
739
|
+
* res.send(403, 'Sorry! you cant see that.');
|
|
740
|
+
* }
|
|
741
|
+
* });
|
|
742
|
+
* });
|
|
743
|
+
*
|
|
744
|
+
* @api public
|
|
745
|
+
*/
|
|
746
|
+
sendFile(path: string, fn?: Errback): void;
|
|
747
|
+
sendFile(path: string, options: SendFileOptions, fn?: Errback): void;
|
|
748
|
+
/**
|
|
749
|
+
* Transfer the file at the given `path` as an attachment.
|
|
750
|
+
*
|
|
751
|
+
* Optionally providing an alternate attachment `filename`,
|
|
752
|
+
* and optional callback `fn(err)`. The callback is invoked
|
|
753
|
+
* when the data transfer is complete, or when an error has
|
|
754
|
+
* ocurred. Be sure to check `res.headersSent` if you plan to respond.
|
|
755
|
+
*
|
|
756
|
+
* The optional options argument passes through to the underlying
|
|
757
|
+
* res.sendFile() call, and takes the exact same parameters.
|
|
758
|
+
*
|
|
759
|
+
* This method uses `res.sendFile()`.
|
|
760
|
+
*/
|
|
761
|
+
download(path: string, fn?: Errback): void;
|
|
762
|
+
download(path: string, filename: string, fn?: Errback): void;
|
|
763
|
+
download(path: string, filename: string, options: DownloadOptions, fn?: Errback): void;
|
|
764
|
+
/**
|
|
765
|
+
* Set _Content-Type_ response header with `type` through `mime.lookup()`
|
|
766
|
+
* when it does not contain "/", or set the Content-Type to `type` otherwise.
|
|
767
|
+
*
|
|
768
|
+
* Examples:
|
|
769
|
+
*
|
|
770
|
+
* res.type('.html');
|
|
771
|
+
* res.type('html');
|
|
772
|
+
* res.type('json');
|
|
773
|
+
* res.type('application/json');
|
|
774
|
+
* res.type('png');
|
|
775
|
+
*/
|
|
776
|
+
contentType(type: string): this;
|
|
777
|
+
/**
|
|
778
|
+
* Set _Content-Type_ response header with `type` through `mime.lookup()`
|
|
779
|
+
* when it does not contain "/", or set the Content-Type to `type` otherwise.
|
|
780
|
+
*
|
|
781
|
+
* Examples:
|
|
782
|
+
*
|
|
783
|
+
* res.type('.html');
|
|
784
|
+
* res.type('html');
|
|
785
|
+
* res.type('json');
|
|
786
|
+
* res.type('application/json');
|
|
787
|
+
* res.type('png');
|
|
788
|
+
*/
|
|
789
|
+
type(type: string): this;
|
|
790
|
+
/**
|
|
791
|
+
* Respond to the Acceptable formats using an `obj`
|
|
792
|
+
* of mime-type callbacks.
|
|
793
|
+
*
|
|
794
|
+
* This method uses `req.accepted`, an array of
|
|
795
|
+
* acceptable types ordered by their quality values.
|
|
796
|
+
* When "Accept" is not present the _first_ callback
|
|
797
|
+
* is invoked, otherwise the first match is used. When
|
|
798
|
+
* no match is performed the server responds with
|
|
799
|
+
* 406 "Not Acceptable".
|
|
800
|
+
*
|
|
801
|
+
* Content-Type is set for you, however if you choose
|
|
802
|
+
* you may alter this within the callback using `res.type()`
|
|
803
|
+
* or `res.set('Content-Type', ...)`.
|
|
804
|
+
*
|
|
805
|
+
* res.format({
|
|
806
|
+
* 'text/plain': function(){
|
|
807
|
+
* res.send('hey');
|
|
808
|
+
* },
|
|
809
|
+
*
|
|
810
|
+
* 'text/html': function(){
|
|
811
|
+
* res.send('<p>hey</p>');
|
|
812
|
+
* },
|
|
813
|
+
*
|
|
814
|
+
* 'appliation/json': function(){
|
|
815
|
+
* res.send({ message: 'hey' });
|
|
816
|
+
* }
|
|
817
|
+
* });
|
|
818
|
+
*
|
|
819
|
+
* In addition to canonicalized MIME types you may
|
|
820
|
+
* also use extnames mapped to these types:
|
|
821
|
+
*
|
|
822
|
+
* res.format({
|
|
823
|
+
* text: function(){
|
|
824
|
+
* res.send('hey');
|
|
825
|
+
* },
|
|
826
|
+
*
|
|
827
|
+
* html: function(){
|
|
828
|
+
* res.send('<p>hey</p>');
|
|
829
|
+
* },
|
|
830
|
+
*
|
|
831
|
+
* json: function(){
|
|
832
|
+
* res.send({ message: 'hey' });
|
|
833
|
+
* }
|
|
834
|
+
* });
|
|
835
|
+
*
|
|
836
|
+
* By default Express passes an `Error`
|
|
837
|
+
* with a `.status` of 406 to `next(err)`
|
|
838
|
+
* if a match is not made. If you provide
|
|
839
|
+
* a `.default` callback it will be invoked
|
|
840
|
+
* instead.
|
|
841
|
+
*/
|
|
842
|
+
format(obj: any): this;
|
|
843
|
+
/**
|
|
844
|
+
* Set _Content-Disposition_ header to _attachment_ with optional `filename`.
|
|
845
|
+
*/
|
|
846
|
+
attachment(filename?: string): this;
|
|
847
|
+
/**
|
|
848
|
+
* Set header `field` to `val`, or pass
|
|
849
|
+
* an object of header fields.
|
|
850
|
+
*
|
|
851
|
+
* Examples:
|
|
852
|
+
*
|
|
853
|
+
* res.set('Foo', ['bar', 'baz']);
|
|
854
|
+
* res.set('Accept', 'application/json');
|
|
855
|
+
* res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
|
|
856
|
+
*
|
|
857
|
+
* Aliased as `res.header()`.
|
|
858
|
+
*/
|
|
859
|
+
set(field: any): this;
|
|
860
|
+
set(field: string, value?: string | string[]): this;
|
|
861
|
+
header(field: any): this;
|
|
862
|
+
header(field: string, value?: string | string[]): this; // Property indicating if HTTP headers has been sent for the response.
|
|
863
|
+
headersSent: boolean;
|
|
864
|
+
/** Get value for header `field`. */
|
|
865
|
+
get(field: string): string | undefined;
|
|
866
|
+
/** Clear cookie `name`. */
|
|
867
|
+
clearCookie(name: string, options?: CookieOptions$1): this;
|
|
868
|
+
/**
|
|
869
|
+
* Set cookie `name` to `val`, with the given `options`.
|
|
870
|
+
*
|
|
871
|
+
* Options:
|
|
872
|
+
*
|
|
873
|
+
* - `maxAge` max-age in milliseconds, converted to `expires`
|
|
874
|
+
* - `signed` sign the cookie
|
|
875
|
+
* - `path` defaults to "/"
|
|
876
|
+
*
|
|
877
|
+
* Examples:
|
|
878
|
+
*
|
|
879
|
+
* // "Remember Me" for 15 minutes
|
|
880
|
+
* res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });
|
|
881
|
+
*
|
|
882
|
+
* // save as above
|
|
883
|
+
* res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })
|
|
884
|
+
*/
|
|
885
|
+
cookie(name: string, val: string, options: CookieOptions$1): this;
|
|
886
|
+
cookie(name: string, val: any, options: CookieOptions$1): this;
|
|
887
|
+
cookie(name: string, val: any): this;
|
|
888
|
+
/**
|
|
889
|
+
* Set the location header to `url`.
|
|
890
|
+
*
|
|
891
|
+
* Examples:
|
|
892
|
+
*
|
|
893
|
+
* res.location('/foo/bar').;
|
|
894
|
+
* res.location('http://example.com');
|
|
895
|
+
* res.location('../login'); // /blog/post/1 -> /blog/login
|
|
896
|
+
*
|
|
897
|
+
* Mounting:
|
|
898
|
+
*
|
|
899
|
+
* When an application is mounted and `res.location()`
|
|
900
|
+
* is given a path that does _not_ lead with "/" it becomes
|
|
901
|
+
* relative to the mount-point. For example if the application
|
|
902
|
+
* is mounted at "/blog", the following would become "/blog/login".
|
|
903
|
+
*
|
|
904
|
+
* res.location('login');
|
|
905
|
+
*
|
|
906
|
+
* While the leading slash would result in a location of "/login":
|
|
907
|
+
*
|
|
908
|
+
* res.location('/login');
|
|
909
|
+
*/
|
|
910
|
+
location(url: string): this;
|
|
911
|
+
/**
|
|
912
|
+
* Redirect to the given `url` with optional response `status`
|
|
913
|
+
* defaulting to 302.
|
|
914
|
+
*
|
|
915
|
+
* The resulting `url` is determined by `res.location()`, so
|
|
916
|
+
* it will play nicely with mounted apps, relative paths, etc.
|
|
917
|
+
*
|
|
918
|
+
* Examples:
|
|
919
|
+
*
|
|
920
|
+
* res.redirect('/foo/bar');
|
|
921
|
+
* res.redirect('http://example.com');
|
|
922
|
+
* res.redirect(301, 'http://example.com');
|
|
923
|
+
* res.redirect('../login'); // /blog/post/1 -> /blog/login
|
|
924
|
+
*/
|
|
925
|
+
redirect(url: string): void;
|
|
926
|
+
redirect(status: number, url: string): void;
|
|
927
|
+
/**
|
|
928
|
+
* Render `view` with the given `options` and optional callback `fn`.
|
|
929
|
+
* When a callback function is given a response will _not_ be made
|
|
930
|
+
* automatically, otherwise a response of _200_ and _text/html_ is given.
|
|
931
|
+
*
|
|
932
|
+
* Options:
|
|
933
|
+
*
|
|
934
|
+
* - `cache` boolean hinting to the engine it should cache
|
|
935
|
+
* - `filename` filename of the view being rendered
|
|
936
|
+
*/
|
|
937
|
+
render(view: string, options?: object, callback?: (err: Error, html: string) => void): void;
|
|
938
|
+
render(view: string, callback?: (err: Error, html: string) => void): void;
|
|
939
|
+
locals: LocalsObj & Locals;
|
|
940
|
+
charset: string;
|
|
941
|
+
/**
|
|
942
|
+
* Adds the field to the Vary response header, if it is not there already.
|
|
943
|
+
* Examples:
|
|
944
|
+
*
|
|
945
|
+
* res.vary('User-Agent').render('docs');
|
|
946
|
+
*/
|
|
947
|
+
vary(field: string): this;
|
|
948
|
+
app: Application$1;
|
|
949
|
+
/**
|
|
950
|
+
* Appends the specified value to the HTTP response header field.
|
|
951
|
+
* If the header is not already set, it creates the header with the specified value.
|
|
952
|
+
* The value parameter can be a string or an array.
|
|
953
|
+
*
|
|
954
|
+
* Note: calling res.set() after res.append() will reset the previously-set header value.
|
|
955
|
+
*
|
|
956
|
+
* @since 4.11.0
|
|
957
|
+
*/
|
|
958
|
+
append(field: string, value?: string[] | string): this;
|
|
959
|
+
/**
|
|
960
|
+
* After middleware.init executed, Response will contain req property
|
|
961
|
+
* See: express/lib/middleware/init.js
|
|
962
|
+
*/
|
|
963
|
+
req: Request$1;
|
|
964
|
+
}
|
|
965
|
+
type RequestParamHandler = (req: Request$1, res: Response$1, next: NextFunction, value: any, name: string) => any;
|
|
966
|
+
type ApplicationRequestHandler<T> = IRouterHandler<T> & IRouterMatcher<T> & ((...handlers: RequestHandlerParams[]) => T);
|
|
967
|
+
interface Application$1<LocalsObj extends Record<string, any> = Record<string, any>> extends EventEmitter, IRouter, Express.Application {
|
|
968
|
+
/**
|
|
969
|
+
* Express instance itself is a request handler, which could be invoked without
|
|
970
|
+
* third argument.
|
|
971
|
+
*/
|
|
972
|
+
(req: Request$1 | http.IncomingMessage, res: Response$1 | http.ServerResponse): any;
|
|
973
|
+
/**
|
|
974
|
+
* Initialize the server.
|
|
975
|
+
*
|
|
976
|
+
* - setup default configuration
|
|
977
|
+
* - setup default middleware
|
|
978
|
+
* - setup route reflection methods
|
|
979
|
+
*/
|
|
980
|
+
init(): void;
|
|
981
|
+
/**
|
|
982
|
+
* Initialize application configuration.
|
|
983
|
+
*/
|
|
984
|
+
defaultConfiguration(): void;
|
|
985
|
+
/**
|
|
986
|
+
* Register the given template engine callback `fn`
|
|
987
|
+
* as `ext`.
|
|
988
|
+
*
|
|
989
|
+
* By default will `require()` the engine based on the
|
|
990
|
+
* file extension. For example if you try to render
|
|
991
|
+
* a "foo.jade" file Express will invoke the following internally:
|
|
992
|
+
*
|
|
993
|
+
* app.engine('jade', require('jade').__express);
|
|
994
|
+
*
|
|
995
|
+
* For engines that do not provide `.__express` out of the box,
|
|
996
|
+
* or if you wish to "map" a different extension to the template engine
|
|
997
|
+
* you may use this method. For example mapping the EJS template engine to
|
|
998
|
+
* ".html" files:
|
|
999
|
+
*
|
|
1000
|
+
* app.engine('html', require('ejs').renderFile);
|
|
1001
|
+
*
|
|
1002
|
+
* In this case EJS provides a `.renderFile()` method with
|
|
1003
|
+
* the same signature that Express expects: `(path, options, callback)`,
|
|
1004
|
+
* though note that it aliases this method as `ejs.__express` internally
|
|
1005
|
+
* so if you're using ".ejs" extensions you dont need to do anything.
|
|
1006
|
+
*
|
|
1007
|
+
* Some template engines do not follow this convention, the
|
|
1008
|
+
* [Consolidate.js](https://github.com/visionmedia/consolidate.js)
|
|
1009
|
+
* library was created to map all of node's popular template
|
|
1010
|
+
* engines to follow this convention, thus allowing them to
|
|
1011
|
+
* work seamlessly within Express.
|
|
1012
|
+
*/
|
|
1013
|
+
engine(ext: string, fn: (path: string, options: object, callback: (e: any, rendered?: string) => void) => void): this;
|
|
1014
|
+
/**
|
|
1015
|
+
* Assign `setting` to `val`, or return `setting`'s value.
|
|
1016
|
+
*
|
|
1017
|
+
* app.set('foo', 'bar');
|
|
1018
|
+
* app.get('foo');
|
|
1019
|
+
* // => "bar"
|
|
1020
|
+
* app.set('foo', ['bar', 'baz']);
|
|
1021
|
+
* app.get('foo');
|
|
1022
|
+
* // => ["bar", "baz"]
|
|
1023
|
+
*
|
|
1024
|
+
* Mounted servers inherit their parent server's settings.
|
|
1025
|
+
*/
|
|
1026
|
+
set(setting: string, val: any): this;
|
|
1027
|
+
get: ((name: string) => any) & IRouterMatcher<this>;
|
|
1028
|
+
param(name: string | string[], handler: RequestParamHandler): this;
|
|
1029
|
+
/**
|
|
1030
|
+
* Return the app's absolute pathname
|
|
1031
|
+
* based on the parent(s) that have
|
|
1032
|
+
* mounted it.
|
|
1033
|
+
*
|
|
1034
|
+
* For example if the application was
|
|
1035
|
+
* mounted as "/admin", which itself
|
|
1036
|
+
* was mounted as "/blog" then the
|
|
1037
|
+
* return value would be "/blog/admin".
|
|
1038
|
+
*/
|
|
1039
|
+
path(): string;
|
|
1040
|
+
/**
|
|
1041
|
+
* Check if `setting` is enabled (truthy).
|
|
1042
|
+
*
|
|
1043
|
+
* app.enabled('foo')
|
|
1044
|
+
* // => false
|
|
1045
|
+
*
|
|
1046
|
+
* app.enable('foo')
|
|
1047
|
+
* app.enabled('foo')
|
|
1048
|
+
* // => true
|
|
1049
|
+
*/
|
|
1050
|
+
enabled(setting: string): boolean;
|
|
1051
|
+
/**
|
|
1052
|
+
* Check if `setting` is disabled.
|
|
1053
|
+
*
|
|
1054
|
+
* app.disabled('foo')
|
|
1055
|
+
* // => true
|
|
1056
|
+
*
|
|
1057
|
+
* app.enable('foo')
|
|
1058
|
+
* app.disabled('foo')
|
|
1059
|
+
* // => false
|
|
1060
|
+
*/
|
|
1061
|
+
disabled(setting: string): boolean;
|
|
1062
|
+
/** Enable `setting`. */
|
|
1063
|
+
enable(setting: string): this;
|
|
1064
|
+
/** Disable `setting`. */
|
|
1065
|
+
disable(setting: string): this;
|
|
1066
|
+
/**
|
|
1067
|
+
* Render the given view `name` name with `options`
|
|
1068
|
+
* and a callback accepting an error and the
|
|
1069
|
+
* rendered template string.
|
|
1070
|
+
*
|
|
1071
|
+
* Example:
|
|
1072
|
+
*
|
|
1073
|
+
* app.render('email', { name: 'Tobi' }, function(err, html){
|
|
1074
|
+
* // ...
|
|
1075
|
+
* })
|
|
1076
|
+
*/
|
|
1077
|
+
render(name: string, options?: object, callback?: (err: Error, html: string) => void): void;
|
|
1078
|
+
render(name: string, callback: (err: Error, html: string) => void): void;
|
|
1079
|
+
/**
|
|
1080
|
+
* Listen for connections.
|
|
1081
|
+
*
|
|
1082
|
+
* A node `http.Server` is returned, with this
|
|
1083
|
+
* application (which is a `Function`) as its
|
|
1084
|
+
* callback. If you wish to create both an HTTP
|
|
1085
|
+
* and HTTPS server you may do so with the "http"
|
|
1086
|
+
* and "https" modules as shown here:
|
|
1087
|
+
*
|
|
1088
|
+
* var http = require('http')
|
|
1089
|
+
* , https = require('https')
|
|
1090
|
+
* , express = require('express')
|
|
1091
|
+
* , app = express();
|
|
1092
|
+
*
|
|
1093
|
+
* http.createServer(app).listen(80);
|
|
1094
|
+
* https.createServer({ ... }, app).listen(443);
|
|
1095
|
+
*/
|
|
1096
|
+
listen(port: number, hostname: string, backlog: number, callback?: (error?: Error) => void): http.Server;
|
|
1097
|
+
listen(port: number, hostname: string, callback?: (error?: Error) => void): http.Server;
|
|
1098
|
+
listen(port: number, callback?: (error?: Error) => void): http.Server;
|
|
1099
|
+
listen(callback?: (error?: Error) => void): http.Server;
|
|
1100
|
+
listen(path: string, callback?: (error?: Error) => void): http.Server;
|
|
1101
|
+
listen(handle: any, listeningListener?: (error?: Error) => void): http.Server;
|
|
1102
|
+
router: Router;
|
|
1103
|
+
settings: any;
|
|
1104
|
+
resource: any;
|
|
1105
|
+
map: any;
|
|
1106
|
+
locals: LocalsObj & Locals;
|
|
1107
|
+
/**
|
|
1108
|
+
* The app.routes object houses all of the routes defined mapped by the
|
|
1109
|
+
* associated HTTP verb. This object may be used for introspection
|
|
1110
|
+
* capabilities, for example Express uses this internally not only for
|
|
1111
|
+
* routing but to provide default OPTIONS behaviour unless app.options()
|
|
1112
|
+
* is used. Your application or framework may also remove routes by
|
|
1113
|
+
* simply by removing them from this object.
|
|
1114
|
+
*/
|
|
1115
|
+
routes: any;
|
|
1116
|
+
/**
|
|
1117
|
+
* Used to get all registered routes in Express Application
|
|
1118
|
+
*/
|
|
1119
|
+
_router: any;
|
|
1120
|
+
use: ApplicationRequestHandler<this>;
|
|
1121
|
+
/**
|
|
1122
|
+
* The mount event is fired on a sub-app, when it is mounted on a parent app.
|
|
1123
|
+
* The parent app is passed to the callback function.
|
|
1124
|
+
*
|
|
1125
|
+
* NOTE:
|
|
1126
|
+
* Sub-apps will:
|
|
1127
|
+
* - Not inherit the value of settings that have a default value. You must set the value in the sub-app.
|
|
1128
|
+
* - Inherit the value of settings with no default value.
|
|
1129
|
+
*/
|
|
1130
|
+
on: (event: "mount", callback: (parent: Application$1) => void) => this;
|
|
1131
|
+
/**
|
|
1132
|
+
* The app.mountpath property contains one or more path patterns on which a sub-app was mounted.
|
|
1133
|
+
*/
|
|
1134
|
+
mountpath: string | string[];
|
|
1135
|
+
}
|
|
1136
|
+
interface Express extends Application$1 {
|
|
1137
|
+
request: Request$1;
|
|
1138
|
+
response: Response$1;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
declare function createApp(callback?: () => void | Promise<void>): Express;
|
|
1142
|
+
|
|
1143
|
+
declare const cspNonce: (req: IncomingMessage) => string;
|
|
1144
|
+
|
|
1145
|
+
declare function emitLog(app: Application, message?: LogEventPayload["message"] | LogEventPayload, payload?: LogEventPayload): any;
|
|
1146
|
+
|
|
1147
|
+
declare function getStatusMessage(prefix: string, statusCode: number): string;
|
|
1148
|
+
|
|
1149
|
+
declare const injectNonce: TransformContent;
|
|
1150
|
+
|
|
1151
|
+
declare const renderStatus: RenderStatus;
|
|
1152
|
+
|
|
1153
|
+
declare function serializeState(state: unknown): string;
|
|
1154
|
+
|
|
1155
|
+
type PipeableStream = {
|
|
1156
|
+
pipe: <Writable extends NodeJS.WritableStream>(destination: Writable) => Writable;
|
|
1157
|
+
};
|
|
1158
|
+
declare function servePipeableStream(req: Request, res: Response): ({
|
|
1159
|
+
pipe
|
|
1160
|
+
}: PipeableStream, error?: unknown) => Promise<void>;
|
|
1161
|
+
|
|
1162
|
+
export { Controller, DirParams, ErrorController, LangParams, LogEventPayload, LogLevel, LogOptions, Middleware, MiddlewareSet, RenderStatus, ReqCtx, ResolveFilePathParams, TransformContent, build, cli, createApp, cspNonce, dir$1 as dir, emitLog, getEffectiveLocale, getLocales, getStatusMessage, init, injectNonce, lang$1 as lang, log, renderStatus, requestEvents, resolveFilePath, serializeState, servePipeableStream, start, toLanguage, unhandledError, unhandledRoute };
|