Skip to content

Commit

Permalink
(doc) document serveFile
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed Aug 6, 2010
1 parent 4005ea5 commit 76896a5
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions README.md
Expand Up @@ -48,9 +48,9 @@ You can also specify how long the client is supposed to cache the files node-sta
This will set the `Cache-Control` header, telling clients to cache the file for an hour.
This is the default setting.

### Serving files #
### Serving files under a directory #

To serve files, simply call the `serve` method on a `Server` instance, passing it
To serve files under a directory, simply call the `serve` method on a `Server` instance, passing it
the HTTP request and response object:

var fileServer = new static.Server('./public');
Expand All @@ -61,6 +61,27 @@ the HTTP request and response object:
});
}).listen(8080);

### Serving specific files #

If you want to serve a specific file, like an error page for example, use the `serveFile` method:

fileServer.serveFile('/error.html', request, response);

This will serve the `error.html` file, from under the file root directory. For example, you could
serve an error page, when the initial request wasn't found:

require('http').createServer(function (request, response) {
request.addListener('end', function () {
fileServer.serve(request, response, function (e, res) {
if (e && (e.status === 404)) { // If the file wasn't found
fileServer.serveFile('/not-found.html', request, response);
}
});
});
}).listen(8080);

More on intercepting errors bellow.

### Intercepting errors & Listening #

An optional callback can be passed as last argument, it will be called every time a file
Expand Down

0 comments on commit 76896a5

Please sign in to comment.