UNPKG

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