4 > a simple, *rfc 2616 compliant* file streaming module for [node](http://nodejs.org)
6 node-static understands and supports *conditional GET* and *HEAD* requests.
7 node-static was inspired by some of the other static-file serving modules out there,
8 such as node-paperboy and antinode.
14 var static = require('node-static');
17 // Create a node-static server instance to serve the './public' folder
19 var file = new static.Server('./public');
21 require('http').createServer(function (request, response) {
22 request.addListener('end', function () {
26 file.serve(request, response);
34 ### Creating a node-static Server #
36 Creating a file server instance is as simple as:
42 This will serve files in the current directory. If you want to serve files in a specific
43 directory, pass it as the first argument:
46 new static.Server('./public');
49 You can also specify how long the client is supposed to cache the files node-static serves:
52 new static.Server('./public', { cache: 3600 });
55 This will set the `Cache-Control` header, telling clients to cache the file for an hour.
56 This is the default setting.
58 ### Serving files under a directory #
60 To serve files under a directory, simply call the `serve` method on a `Server` instance, passing it
61 the HTTP request and response object:
64 var static = require('node-static');
66 var fileServer = new static.Server('./public');
68 require('http').createServer(function (request, response) {
69 request.addListener('end', function () {
70 fileServer.serve(request, response);
75 ### Serving specific files #
77 If you want to serve a specific file, like an error page for example, use the `serveFile` method:
80 fileServer.serveFile('/error.html', 500, {}, request, response);
83 This will serve the `error.html` file, from under the file root directory, with a `500` status code.
84 For example, you could serve an error page, when the initial request wasn't found:
87 require('http').createServer(function (request, response) {
88 request.addListener('end', function () {
89 fileServer.serve(request, response, function (e, res) {
90 if (e && (e.status === 404)) { // If the file wasn't found
91 fileServer.serveFile('/not-found.html', 404, {}, request, response);
98 More on intercepting errors bellow.
100 ### Intercepting errors & Listening #
102 An optional callback can be passed as last argument, it will be called every time a file
103 has been served successfully, or if there was an error serving the file:
106 var static = require('node-static');
108 var fileServer = new static.Server('./public');
110 require('http').createServer(function (request, response) {
111 request.addListener('end', function () {
112 fileServer.serve(request, response, function (err, result) {
113 if (err) { // There was an error serving the file
114 console.error("Error serving " + request.url + " - " + err.message);
116 // Respond to the client
117 response.writeHead(err.status, err.headers);
125 Note that if you pass a callback, and there is an error serving the file, node-static
126 *will not* respond to the client. This gives you the opportunity to re-route the request,
127 or handle it differently.
129 For example, you may want to interpret a request as a static request, but if the file isn't found,
130 send it to an application.
132 If you only want to *listen* for errors, you can use *event listeners*:
135 fileServer.serve(request, response).addListener('error', function (err) {
136 console.error("Error serving " + request.url + " - " + err.message);
140 With this method, you don't have to explicitly send the response back, in case of an error.
142 ### Options when creating an instance of `Server` #
146 Sets the `Cache-Control` header.
148 example: `{ cache: 7200 }`
150 Passing a number will set the cache duration to that number of seconds.
151 Passing `false` will disable the `Cache-Control` header.
158 Sets the `Server` header.
160 example: `{ serverInfo: "myserver" }`
162 > Defaults to `node-static/{version}`
166 Sets response headers.
168 example: `{ 'X-Hello': 'World!' }`
174 Enable support for sending compressed responses. This will enable a check for a
175 file with the same name plus '.gz' in the same folder. If the compressed file is
176 found and the client has indicated support for gzip file transfer, the contents
177 of the .gz file will be sent in place of the uncompressed file along with a
178 Content-Encoding: gzip header to inform the client the data has been compressed.
180 example: `{ gzip: true }`
181 example: `{ gzip: /^\/text/ }`
183 Passing `true` will enable this check for all files.
184 Passing a RegExp instance will only enable this check if the content-type of the
185 respond would match that RegExp using its test() method.
187 > Defaults to `false`
191 Choose a custom index file when serving up directories.
193 example: `{ indexFile: "index.htm" }`
195 > Defaults to `index.html`
198 Command Line Interface
199 ----------------------
201 `node-static` also provides a CLI.
206 $ npm install -g node-static
212 # serve up the current directory
214 serving "." at http://127.0.0.1:8080
216 # serve up a different directory
218 serving "public" at http://127.0.0.1:8080
220 # specify additional headers (this one is useful for development)
221 $ static -H '{"Cache-Control": "no-cache, must-revalidate"}'
222 serving "." at http://127.0.0.1:8080
224 # set cache control max age
226 serving "." at http://127.0.0.1:8080
228 # expose the server to your local network
230 serving "." at http://0.0.0.0:8080
232 # show help message, including all options