source: node_modules/serve-static/README.md

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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