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 Set how "dotfiles" are treated when encountered. A dotfile is a file
35 or directory that begins with a dot ("."). Note this check is done on
36 the path itself without checking if the path actually exists on the
37 disk. If `root` is specified, only the dotfiles above the root are
38 checked (i.e. the root itself can be within a dotfile when set
41 The default value is `'ignore'`.
43 - `'allow'` No special treatment for dotfiles.
44 - `'deny'` Send a 403 for any request for a dotfile.
45 - `'ignore'` Pretend like the dotfile does not exist and call `next()`.
49 Enable or disable etag generation, defaults to true.
53 Set file extension fallbacks. When set, if a file is not found, the given
54 extensions will be added to the file name and search for. The first that
55 exists will be served. Example: `['html', 'htm']`.
57 The default value is `false`.
61 By default this module will send "index.html" files in response to a request
62 on a directory. To disable this set `false` or to supply a new index pass a
63 string or an array in preferred order.
67 Enable or disable `Last-Modified` header, defaults to true. Uses the file
68 system's last modified value.
72 Provide a max-age in milliseconds for http caching, defaults to 0. This
73 can also be a string accepted by the [ms](https://www.npmjs.org/package/ms#readme)
78 Redirect to trailing "/" when the pathname is a dir. Defaults to `true`.
82 Function to set custom headers on response. Alterations to the headers need to
83 occur synchronously. The function is called as `fn(res, path, stat)`, where
86 - `res` the response object
87 - `path` the file path that is being sent
88 - `stat` the stat object of the file that is being sent
92 ### Serve files with vanilla node.js http server
95 var finalhandler = require('finalhandler')
96 var http = require('http')
97 var serveStatic = require('serve-static')
99 // Serve up public/ftp folder
100 var serve = serveStatic('public/ftp', {'index': ['index.html', 'index.htm']})
103 var server = http.createServer(function(req, res){
104 var done = finalhandler(req, res)
105 serve(req, res, done)
112 ### Serve all files as downloads
115 var contentDisposition = require('content-disposition')
116 var finalhandler = require('finalhandler')
117 var http = require('http')
118 var serveStatic = require('serve-static')
120 // Serve up public/ftp folder
121 var serve = serveStatic('public/ftp', {
123 'setHeaders': setHeaders
126 // Set header to force download
127 function setHeaders(res, path) {
128 res.setHeader('Content-Disposition', contentDisposition(path))
132 var server = http.createServer(function(req, res){
133 var done = finalhandler(req, res)
134 serve(req, res, done)
141 ### Serving using express
144 var express = require('express')
145 var serveStatic = require('serve-static')
149 app.use(serveStatic('public/ftp', {'index': ['default.html', 'default.htm']}))
157 [npm-image]: https://img.shields.io/npm/v/serve-static.svg?style=flat
158 [npm-url]: https://npmjs.org/package/serve-static
159 [travis-image]: https://img.shields.io/travis/expressjs/serve-static/master.svg?label=linux&style=flat
160 [travis-url]: https://travis-ci.org/expressjs/serve-static
161 [appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/serve-static/master.svg?label=windows&style=flat
162 [appveyor-url]: https://ci.appveyor.com/project/dougwilson/serve-static
163 [coveralls-image]: https://img.shields.io/coveralls/expressjs/serve-static/master.svg?style=flat
164 [coveralls-url]: https://coveralls.io/r/expressjs/serve-static
165 [downloads-image]: https://img.shields.io/npm/dm/serve-static.svg?style=flat
166 [downloads-url]: https://npmjs.org/package/serve-static
167 [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg?style=flat
168 [gratipay-url]: https://gratipay.com/dougwilson/