3bd75f3ae8f9f3f8af5a9a7fd69d62e3fccad5c5
[aai/esr-gui.git] /
1 # serve-static
2
3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Linux Build][travis-image]][travis-url]
6 [![Windows Build][appveyor-image]][appveyor-url]
7 [![Test Coverage][coveralls-image]][coveralls-url]
8 [![Gratipay][gratipay-image]][gratipay-url]
9
10 ## Install
11
12 ```sh
13 $ npm install serve-static
14 ```
15
16 ## API
17
18 ```js
19 var serveStatic = require('serve-static')
20 ```
21
22 ### serveStatic(root, options)
23
24 Create a new middleware function to serve files from within a given root
25 directory. The file to serve will be determined by combining `req.url`
26 with the provided root directory. When a file is not found, instead of
27 sending a 404 response, this module will instead call `next()` to move on
28 to the next middleware, allowing for stacking and fall-backs.
29
30 #### Options
31
32 ##### acceptRanges
33
34 Enable or disable accepting ranged requests, defaults to true.
35 Disabling this will not send `Accept-Ranges` and ignore the contents
36 of the `Range` request header.
37
38 ##### cacheControl
39
40 Enable or disable setting `Cache-Control` response header, defaults to
41 true. Disabling this will ignore the `maxAge` option.
42
43 ##### dotfiles
44
45  Set how "dotfiles" are treated when encountered. A dotfile is a file
46 or directory that begins with a dot ("."). Note this check is done on
47 the path itself without checking if the path actually exists on the
48 disk. If `root` is specified, only the dotfiles above the root are
49 checked (i.e. the root itself can be within a dotfile when set
50 to "deny").
51
52   - `'allow'` No special treatment for dotfiles.
53   - `'deny'` Deny a request for a dotfile and 403/`next()`.
54   - `'ignore'` Pretend like the dotfile does not exist and 404/`next()`.
55
56 The default value is similar to `'ignore'`, with the exception that this
57 default will not ignore the files within a directory that begins with a dot.
58
59 ##### etag
60
61 Enable or disable etag generation, defaults to true.
62
63 ##### extensions
64
65 Set file extension fallbacks. When set, if a file is not found, the given
66 extensions will be added to the file name and search for. The first that
67 exists will be served. Example: `['html', 'htm']`.
68
69 The default value is `false`.
70
71 ##### fallthrough
72
73 Set the middleware to have client errors fall-through as just unhandled
74 requests, otherwise forward a client error. The difference is that client
75 errors like a bad request or a request to a non-existent file will cause
76 this middleware to simply `next()` to your next middleware when this value
77 is `true`. When this value is `false`, these errors (even 404s), will invoke
78 `next(err)`.
79
80 Typically `true` is desired such that multiple physical directories can be
81 mapped to the same web address or for routes to fill in non-existent files.
82
83 The value `false` can be used if this middleware is mounted at a path that
84 is designed to be strictly a single file system directory, which allows for
85 short-circuiting 404s for less overhead. This middleware will also reply to
86 all methods.
87
88 The default value is `true`.
89
90 ##### index
91
92 By default this module will send "index.html" files in response to a request
93 on a directory. To disable this set `false` or to supply a new index pass a
94 string or an array in preferred order.
95
96 ##### lastModified
97
98 Enable or disable `Last-Modified` header, defaults to true. Uses the file
99 system's last modified value.
100
101 ##### maxAge
102
103 Provide a max-age in milliseconds for http caching, defaults to 0. This
104 can also be a string accepted by the [ms](https://www.npmjs.org/package/ms#readme)
105 module.
106
107 ##### redirect
108
109 Redirect to trailing "/" when the pathname is a dir. Defaults to `true`.
110
111 ##### setHeaders
112
113 Function to set custom headers on response. Alterations to the headers need to
114 occur synchronously. The function is called as `fn(res, path, stat)`, where
115 the arguments are:
116
117   - `res` the response object
118   - `path` the file path that is being sent
119   - `stat` the stat object of the file that is being sent
120
121 ## Examples
122
123 ### Serve files with vanilla node.js http server
124
125 ```js
126 var finalhandler = require('finalhandler')
127 var http = require('http')
128 var serveStatic = require('serve-static')
129
130 // Serve up public/ftp folder
131 var serve = serveStatic('public/ftp', {'index': ['index.html', 'index.htm']})
132
133 // Create server
134 var server = http.createServer(function onRequest (req, res) {
135   serve(req, res, finalhandler(req, res))
136 })
137
138 // Listen
139 server.listen(3000)
140 ```
141
142 ### Serve all files as downloads
143
144 ```js
145 var contentDisposition = require('content-disposition')
146 var finalhandler = require('finalhandler')
147 var http = require('http')
148 var serveStatic = require('serve-static')
149
150 // Serve up public/ftp folder
151 var serve = serveStatic('public/ftp', {
152   'index': false,
153   'setHeaders': setHeaders
154 })
155
156 // Set header to force download
157 function setHeaders(res, path) {
158   res.setHeader('Content-Disposition', contentDisposition(path))
159 }
160
161 // Create server
162 var server = http.createServer(function onRequest (req, res) {
163   serve(req, res, finalhandler(req, res))
164 })
165
166 // Listen
167 server.listen(3000)
168 ```
169
170 ### Serving using express
171
172 #### Simple
173
174 This is a simple example of using Express.
175
176 ```js
177 var express = require('express')
178 var serveStatic = require('serve-static')
179
180 var app = express()
181
182 app.use(serveStatic('public/ftp', {'index': ['default.html', 'default.htm']}))
183 app.listen(3000)
184 ```
185
186 #### Multiple roots
187
188 This example shows a simple way to search through multiple directories.
189 Files are look for in `public-optimized/` first, then `public/` second as
190 a fallback.
191
192 ```js
193 var express = require('express')
194 var serveStatic = require('serve-static')
195
196 var app = express()
197
198 app.use(serveStatic(__dirname + '/public-optimized'))
199 app.use(serveStatic(__dirname + '/public'))
200 app.listen(3000)
201 ```
202
203 #### Different settings for paths
204
205 This example shows how to set a different max age depending on the served
206 file type. In this example, HTML files are not cached, while everything else
207 is for 1 day.
208
209 ```js
210 var express = require('express')
211 var serveStatic = require('serve-static')
212
213 var app = express()
214
215 app.use(serveStatic(__dirname + '/public', {
216   maxAge: '1d',
217   setHeaders: setCustomCacheControl
218 }))
219
220 app.listen(3000)
221
222 function setCustomCacheControl (res, path) {
223   if (serveStatic.mime.lookup(path) === 'text/html') {
224     // Custom Cache-Control for HTML files
225     res.setHeader('Cache-Control', 'public, max-age=0')
226   }
227 }
228 ```
229
230 ## License
231
232 [MIT](LICENSE)
233
234 [npm-image]: https://img.shields.io/npm/v/serve-static.svg
235 [npm-url]: https://npmjs.org/package/serve-static
236 [travis-image]: https://img.shields.io/travis/expressjs/serve-static/master.svg?label=linux
237 [travis-url]: https://travis-ci.org/expressjs/serve-static
238 [appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/serve-static/master.svg?label=windows
239 [appveyor-url]: https://ci.appveyor.com/project/dougwilson/serve-static
240 [coveralls-image]: https://img.shields.io/coveralls/expressjs/serve-static/master.svg
241 [coveralls-url]: https://coveralls.io/r/expressjs/serve-static
242 [downloads-image]: https://img.shields.io/npm/dm/serve-static.svg
243 [downloads-url]: https://npmjs.org/package/serve-static
244 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
245 [gratipay-url]: https://gratipay.com/dougwilson/