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