UNPKG

4.68 kBTypeScriptView Raw
1/// <reference types="node" />
2import * as http from "http";
3import { HttpError } from "http-errors";
4import * as send from "send";
5
6/**
7 * Create a new middleware function to serve files from within a given root directory.
8 * The file to serve will be determined by combining req.url with the provided root directory.
9 * When a file is not found, instead of sending a 404 response, this module will instead call next() to move on to the next middleware, allowing for stacking and fall-backs.
10 */
11declare function serveStatic<R extends http.ServerResponse>(
12 root: string,
13 options?: serveStatic.ServeStaticOptions<R>,
14): serveStatic.RequestHandler<R>;
15
16declare namespace serveStatic {
17 var mime: typeof send.mime;
18 interface ServeStaticOptions<R extends http.ServerResponse = http.ServerResponse> {
19 /**
20 * Enable or disable accepting ranged requests, defaults to true.
21 * Disabling this will not send Accept-Ranges and ignore the contents of the Range request header.
22 */
23 acceptRanges?: boolean | undefined;
24
25 /**
26 * Enable or disable setting Cache-Control response header, defaults to true.
27 * Disabling this will ignore the immutable and maxAge options.
28 */
29 cacheControl?: boolean | undefined;
30
31 /**
32 * Set how "dotfiles" are treated when encountered. A dotfile is a file or directory that begins with a dot (".").
33 * Note this check is done on the path itself without checking if the path actually exists on the disk.
34 * If root is specified, only the dotfiles above the root are checked (i.e. the root itself can be within a dotfile when when set to "deny").
35 * The default value is 'ignore'.
36 * 'allow' No special treatment for dotfiles
37 * 'deny' Send a 403 for any request for a dotfile
38 * 'ignore' Pretend like the dotfile does not exist and call next()
39 */
40 dotfiles?: string | undefined;
41
42 /**
43 * Enable or disable etag generation, defaults to true.
44 */
45 etag?: boolean | undefined;
46
47 /**
48 * Set file extension fallbacks. When set, if a file is not found, the given extensions will be added to the file name and search for.
49 * The first that exists will be served. Example: ['html', 'htm'].
50 * The default value is false.
51 */
52 extensions?: string[] | false | undefined;
53
54 /**
55 * Let client errors fall-through as unhandled requests, otherwise forward a client error.
56 * The default value is true.
57 */
58 fallthrough?: boolean | undefined;
59
60 /**
61 * Enable or disable the immutable directive in the Cache-Control response header.
62 * If enabled, the maxAge option should also be specified to enable caching. The immutable directive will prevent supported clients from making conditional requests during the life of the maxAge option to check if the file has changed.
63 */
64 immutable?: boolean | undefined;
65
66 /**
67 * By default this module will send "index.html" files in response to a request on a directory.
68 * To disable this set false or to supply a new index pass a string or an array in preferred order.
69 */
70 index?: boolean | string | string[] | undefined;
71
72 /**
73 * Enable or disable Last-Modified header, defaults to true. Uses the file system's last modified value.
74 */
75 lastModified?: boolean | undefined;
76
77 /**
78 * Provide a max-age in milliseconds for http caching, defaults to 0. This can also be a string accepted by the ms module.
79 */
80 maxAge?: number | string | undefined;
81
82 /**
83 * Redirect to trailing "/" when the pathname is a dir. Defaults to true.
84 */
85 redirect?: boolean | undefined;
86
87 /**
88 * Function to set custom headers on response. Alterations to the headers need to occur synchronously.
89 * The function is called as fn(res, path, stat), where the arguments are:
90 * res the response object
91 * path the file path that is being sent
92 * stat the stat object of the file that is being sent
93 */
94 setHeaders?: ((res: R, path: string, stat: any) => any) | undefined;
95 }
96
97 interface RequestHandler<R extends http.ServerResponse> {
98 (request: http.IncomingMessage, response: R, next: (err?: HttpError) => void): any;
99 }
100
101 interface RequestHandlerConstructor<R extends http.ServerResponse> {
102 (root: string, options?: ServeStaticOptions<R>): RequestHandler<R>;
103 mime: typeof send.mime;
104 }
105}
106
107export = serveStatic;
108
\No newline at end of file