UNPKG

5.17 kBMarkdownView Raw
1node-static
2===========
3
4> a simple, *rfc 2616 compliant* file streaming module for [node](http://nodejs.org)
5
6node-static has an in-memory file cache, making it highly efficient.
7node-static understands and supports *conditional GET* and *HEAD* requests.
8node-static was inspired by some of the other static-file serving modules out there,
9such as node-paperboy and antinode.
10
11Synopsis
12--------
13
14 var static = require('node-static');
15
16 //
17 // Create a node-static server instance to serve the './public' folder
18 //
19 var file = new(static.Server)('./public');
20
21 require('http').createServer(function (request, response) {
22 request.addListener('end', function () {
23 //
24 // Serve files!
25 //
26 file.serve(request, response);
27 });
28 }).listen(8080);
29
30API
31---
32
33### Creating a node-static Server #
34
35Creating a file server instance is as simple as:
36
37 new static.Server();
38
39This will serve files in the current directory. If you want to serve files in a specific
40directory, pass it as the first argument:
41
42 new static.Server('./public');
43
44You can also specify how long the client is supposed to cache the files node-static serves:
45
46 new static.Server('./public', { cache: 3600 });
47
48This will set the `Cache-Control` header, telling clients to cache the file for an hour.
49This is the default setting.
50
51### Serving files under a directory #
52
53To serve files under a directory, simply call the `serve` method on a `Server` instance, passing it
54the HTTP request and response object:
55
56 var fileServer = new static.Server('./public');
57
58 require('http').createServer(function (request, response) {
59 request.addListener('end', function () {
60 fileServer.serve(request, response);
61 });
62 }).listen(8080);
63
64### Serving specific files #
65
66If you want to serve a specific file, like an error page for example, use the `serveFile` method:
67
68 fileServer.serveFile('/error.html', 500, {}, request, response);
69
70This will serve the `error.html` file, from under the file root directory, with a `500` status code.
71For example, you could serve an error page, when the initial request wasn't found:
72
73 require('http').createServer(function (request, response) {
74 request.addListener('end', function () {
75 fileServer.serve(request, response, function (e, res) {
76 if (e && (e.status === 404)) { // If the file wasn't found
77 fileServer.serveFile('/not-found.html', 404, {}, request, response);
78 }
79 });
80 });
81 }).listen(8080);
82
83More on intercepting errors bellow.
84
85### Intercepting errors & Listening #
86
87An optional callback can be passed as last argument, it will be called every time a file
88has been served successfully, or if there was an error serving the file:
89
90 var fileServer = new static.Server('./public');
91
92 require('http').createServer(function (request, response) {
93 request.addListener('end', function () {
94 fileServer.serve(request, response, function (err, result) {
95 if (err) { // There was an error serving the file
96 sys.error("Error serving " + request.url + " - " + err.message);
97
98 // Respond to the client
99 response.writeHead(err.status, err.headers);
100 response.end();
101 }
102 });
103 });
104 }).listen(8080);
105
106Note that if you pass a callback, and there is an error serving the file, node-static
107*will not* respond to the client. This gives you the opportunity to re-route the request,
108or handle it differently.
109
110For example, you may want to interpret a request as a static request, but if the file isn't found,
111send it to an application.
112
113If you only want to *listen* for errors, you can use *event listeners*:
114
115 fileServer.serve(request, response).addListener('error', function (err) {
116 sys.error("Error serving " + request.url + " - " + err.message);
117 });
118
119With this method, you don't have to explicitly send the response back, in case of an error.
120
121### Options when creating an instance of `Server` #
122
123#### `cache` #
124
125Sets the `Cache-Control` header.
126
127example: `{ cache: 7200 }`
128
129Passing a number will set the cache duration to that number of seconds.
130Passing `false` will disable the `Cache-Control` header.
131
132> Defaults to `3600`
133
134#### `headers` #
135
136Sets response headers.
137
138example: `{ 'X-Hello': 'World!' }`
139
140> defaults to `{}`
141
142Command Line Interface
143----------------------
144
145`node-static` also provides a CLI.
146
147### Installation #
148
149 $ npm install -g node-static
150
151### Example Usage #
152
153 # serve up the current directory
154 $ static
155 serving "." at http://127.0.0.1:8080
156
157 # serve up a different directory
158 $ static public
159 serving "public" at http://127.0.0.1:8080
160
161 # specify additional headers (this one is useful for development)
162 $ static -H '{"Cache-Control": "no-cache, must-revaliate"}'
163 serving "." at http://127.0.0.1:8080
164
165 # set cache control max age
166 $ static -c 7200
167 serving "." at http://127.0.0.1:8080
168
169 # show help message, including all options
170 $ static -h