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]
13 $ npm install serve-static
19 var serveStatic = require('serve-static')
22 ### serveStatic(root, options)
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.
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.
40 Enable or disable setting `Cache-Control` response header, defaults to
41 true. Disabling this will ignore the `maxAge` option.
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
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()`.
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.
61 Enable or disable etag generation, defaults to true.
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']`.
69 The default value is `false`.
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
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.
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
88 The default value is `true`.
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.
98 Enable or disable `Last-Modified` header, defaults to true. Uses the file
99 system's last modified value.
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)
109 Redirect to trailing "/" when the pathname is a dir. Defaults to `true`.
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
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
123 ### Serve files with vanilla node.js http server
126 var finalhandler = require('finalhandler')
127 var http = require('http')
128 var serveStatic = require('serve-static')
130 // Serve up public/ftp folder
131 var serve = serveStatic('public/ftp', {'index': ['index.html', 'index.htm']})
134 var server = http.createServer(function onRequest (req, res) {
135 serve(req, res, finalhandler(req, res))
142 ### Serve all files as downloads
145 var contentDisposition = require('content-disposition')
146 var finalhandler = require('finalhandler')
147 var http = require('http')
148 var serveStatic = require('serve-static')
150 // Serve up public/ftp folder
151 var serve = serveStatic('public/ftp', {
153 'setHeaders': setHeaders
156 // Set header to force download
157 function setHeaders(res, path) {
158 res.setHeader('Content-Disposition', contentDisposition(path))
162 var server = http.createServer(function onRequest (req, res) {
163 serve(req, res, finalhandler(req, res))
170 ### Serving using express
174 This is a simple example of using Express.
177 var express = require('express')
178 var serveStatic = require('serve-static')
182 app.use(serveStatic('public/ftp', {'index': ['default.html', 'default.htm']}))
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
193 var express = require('express')
194 var serveStatic = require('serve-static')
198 app.use(serveStatic(__dirname + '/public-optimized'))
199 app.use(serveStatic(__dirname + '/public'))
203 #### Different settings for paths
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
210 var express = require('express')
211 var serveStatic = require('serve-static')
215 app.use(serveStatic(__dirname + '/public', {
217 setHeaders: setCustomCacheControl
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')
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/