[d24f17c] | 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 |
|
---|
| 11 | This 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
|
---|
| 22 | var serveStatic = require('serve-static')
|
---|
| 23 | ```
|
---|
| 24 |
|
---|
| 25 | ### serveStatic(root, options)
|
---|
| 26 |
|
---|
| 27 | Create a new middleware function to serve files from within a given root
|
---|
| 28 | directory. The file to serve will be determined by combining `req.url`
|
---|
| 29 | with the provided root directory. When a file is not found, instead of
|
---|
| 30 | sending a 404 response, this module will instead call `next()` to move on
|
---|
| 31 | to the next middleware, allowing for stacking and fall-backs.
|
---|
| 32 |
|
---|
| 33 | #### Options
|
---|
| 34 |
|
---|
| 35 | ##### acceptRanges
|
---|
| 36 |
|
---|
| 37 | Enable or disable accepting ranged requests, defaults to true.
|
---|
| 38 | Disabling this will not send `Accept-Ranges` and ignore the contents
|
---|
| 39 | of the `Range` request header.
|
---|
| 40 |
|
---|
| 41 | ##### cacheControl
|
---|
| 42 |
|
---|
| 43 | Enable or disable setting `Cache-Control` response header, defaults to
|
---|
| 44 | true. 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
|
---|
| 49 | or directory that begins with a dot ("."). Note this check is done on
|
---|
| 50 | the path itself without checking if the path actually exists on the
|
---|
| 51 | disk. If `root` is specified, only the dotfiles above the root are
|
---|
| 52 | checked (i.e. the root itself can be within a dotfile when set
|
---|
| 53 | to "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 |
|
---|
| 59 | The default value is similar to `'ignore'`, with the exception that this
|
---|
| 60 | default will not ignore the files within a directory that begins with a dot.
|
---|
| 61 |
|
---|
| 62 | ##### etag
|
---|
| 63 |
|
---|
| 64 | Enable or disable etag generation, defaults to true.
|
---|
| 65 |
|
---|
| 66 | ##### extensions
|
---|
| 67 |
|
---|
| 68 | Set file extension fallbacks. When set, if a file is not found, the given
|
---|
| 69 | extensions will be added to the file name and search for. The first that
|
---|
| 70 | exists will be served. Example: `['html', 'htm']`.
|
---|
| 71 |
|
---|
| 72 | The default value is `false`.
|
---|
| 73 |
|
---|
| 74 | ##### fallthrough
|
---|
| 75 |
|
---|
| 76 | Set the middleware to have client errors fall-through as just unhandled
|
---|
| 77 | requests, otherwise forward a client error. The difference is that client
|
---|
| 78 | errors like a bad request or a request to a non-existent file will cause
|
---|
| 79 | this middleware to simply `next()` to your next middleware when this value
|
---|
| 80 | is `true`. When this value is `false`, these errors (even 404s), will invoke
|
---|
| 81 | `next(err)`.
|
---|
| 82 |
|
---|
| 83 | Typically `true` is desired such that multiple physical directories can be
|
---|
| 84 | mapped to the same web address or for routes to fill in non-existent files.
|
---|
| 85 |
|
---|
| 86 | The value `false` can be used if this middleware is mounted at a path that
|
---|
| 87 | is designed to be strictly a single file system directory, which allows for
|
---|
| 88 | short-circuiting 404s for less overhead. This middleware will also reply to
|
---|
| 89 | all methods.
|
---|
| 90 |
|
---|
| 91 | The default value is `true`.
|
---|
| 92 |
|
---|
| 93 | ##### immutable
|
---|
| 94 |
|
---|
| 95 | Enable or disable the `immutable` directive in the `Cache-Control` response
|
---|
| 96 | header, defaults to `false`. If set to `true`, the `maxAge` option should
|
---|
| 97 | also be specified to enable caching. The `immutable` directive will prevent
|
---|
| 98 | supported clients from making conditional requests during the life of the
|
---|
| 99 | `maxAge` option to check if the file has changed.
|
---|
| 100 |
|
---|
| 101 | ##### index
|
---|
| 102 |
|
---|
| 103 | By default this module will send "index.html" files in response to a request
|
---|
| 104 | on a directory. To disable this set `false` or to supply a new index pass a
|
---|
| 105 | string or an array in preferred order.
|
---|
| 106 |
|
---|
| 107 | ##### lastModified
|
---|
| 108 |
|
---|
| 109 | Enable or disable `Last-Modified` header, defaults to true. Uses the file
|
---|
| 110 | system's last modified value.
|
---|
| 111 |
|
---|
| 112 | ##### maxAge
|
---|
| 113 |
|
---|
| 114 | Provide a max-age in milliseconds for http caching, defaults to 0. This
|
---|
| 115 | can also be a string accepted by the [ms](https://www.npmjs.org/package/ms#readme)
|
---|
| 116 | module.
|
---|
| 117 |
|
---|
| 118 | ##### redirect
|
---|
| 119 |
|
---|
| 120 | Redirect to trailing "/" when the pathname is a dir. Defaults to `true`.
|
---|
| 121 |
|
---|
| 122 | ##### setHeaders
|
---|
| 123 |
|
---|
| 124 | Function to set custom headers on response. Alterations to the headers need to
|
---|
| 125 | occur synchronously. The function is called as `fn(res, path, stat)`, where
|
---|
| 126 | the 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
|
---|
| 137 | var finalhandler = require('finalhandler')
|
---|
| 138 | var http = require('http')
|
---|
| 139 | var serveStatic = require('serve-static')
|
---|
| 140 |
|
---|
| 141 | // Serve up public/ftp folder
|
---|
| 142 | var serve = serveStatic('public/ftp', { index: ['index.html', 'index.htm'] })
|
---|
| 143 |
|
---|
| 144 | // Create server
|
---|
| 145 | var server = http.createServer(function onRequest (req, res) {
|
---|
| 146 | serve(req, res, finalhandler(req, res))
|
---|
| 147 | })
|
---|
| 148 |
|
---|
| 149 | // Listen
|
---|
| 150 | server.listen(3000)
|
---|
| 151 | ```
|
---|
| 152 |
|
---|
| 153 | ### Serve all files as downloads
|
---|
| 154 |
|
---|
| 155 | ```js
|
---|
| 156 | var contentDisposition = require('content-disposition')
|
---|
| 157 | var finalhandler = require('finalhandler')
|
---|
| 158 | var http = require('http')
|
---|
| 159 | var serveStatic = require('serve-static')
|
---|
| 160 |
|
---|
| 161 | // Serve up public/ftp folder
|
---|
| 162 | var serve = serveStatic('public/ftp', {
|
---|
| 163 | index: false,
|
---|
| 164 | setHeaders: setHeaders
|
---|
| 165 | })
|
---|
| 166 |
|
---|
| 167 | // Set header to force download
|
---|
| 168 | function setHeaders (res, path) {
|
---|
| 169 | res.setHeader('Content-Disposition', contentDisposition(path))
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | // Create server
|
---|
| 173 | var server = http.createServer(function onRequest (req, res) {
|
---|
| 174 | serve(req, res, finalhandler(req, res))
|
---|
| 175 | })
|
---|
| 176 |
|
---|
| 177 | // Listen
|
---|
| 178 | server.listen(3000)
|
---|
| 179 | ```
|
---|
| 180 |
|
---|
| 181 | ### Serving using express
|
---|
| 182 |
|
---|
| 183 | #### Simple
|
---|
| 184 |
|
---|
| 185 | This is a simple example of using Express.
|
---|
| 186 |
|
---|
| 187 | ```js
|
---|
| 188 | var express = require('express')
|
---|
| 189 | var serveStatic = require('serve-static')
|
---|
| 190 |
|
---|
| 191 | var app = express()
|
---|
| 192 |
|
---|
| 193 | app.use(serveStatic('public/ftp', { index: ['default.html', 'default.htm'] }))
|
---|
| 194 | app.listen(3000)
|
---|
| 195 | ```
|
---|
| 196 |
|
---|
| 197 | #### Multiple roots
|
---|
| 198 |
|
---|
| 199 | This example shows a simple way to search through multiple directories.
|
---|
| 200 | Files are searched for in `public-optimized/` first, then `public/` second
|
---|
| 201 | as a fallback.
|
---|
| 202 |
|
---|
| 203 | ```js
|
---|
| 204 | var express = require('express')
|
---|
| 205 | var path = require('path')
|
---|
| 206 | var serveStatic = require('serve-static')
|
---|
| 207 |
|
---|
| 208 | var app = express()
|
---|
| 209 |
|
---|
| 210 | app.use(serveStatic(path.join(__dirname, 'public-optimized')))
|
---|
| 211 | app.use(serveStatic(path.join(__dirname, 'public')))
|
---|
| 212 | app.listen(3000)
|
---|
| 213 | ```
|
---|
| 214 |
|
---|
| 215 | #### Different settings for paths
|
---|
| 216 |
|
---|
| 217 | This example shows how to set a different max age depending on the served
|
---|
| 218 | file type. In this example, HTML files are not cached, while everything else
|
---|
| 219 | is for 1 day.
|
---|
| 220 |
|
---|
| 221 | ```js
|
---|
| 222 | var express = require('express')
|
---|
| 223 | var path = require('path')
|
---|
| 224 | var serveStatic = require('serve-static')
|
---|
| 225 |
|
---|
| 226 | var app = express()
|
---|
| 227 |
|
---|
| 228 | app.use(serveStatic(path.join(__dirname, 'public'), {
|
---|
| 229 | maxAge: '1d',
|
---|
| 230 | setHeaders: setCustomCacheControl
|
---|
| 231 | }))
|
---|
| 232 |
|
---|
| 233 | app.listen(3000)
|
---|
| 234 |
|
---|
| 235 | function 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
|
---|