UNPKG

4.97 kBMarkdownView Raw
1buffet
2======
3
4Performance-oriented static file server
5
6[![build status](https://secure.travis-ci.org/carlos8f/node-buffet.png)](http://travis-ci.org/carlos8f/node-buffet)
7
8Idea
9----
10
11Serving static files should be the most efficient thing that a Node.js app can
12do. Turns out, runtime syscalls to the filesystem can really hang your page
13loads, especially if your filesystem is networked or unreliable in some other way.
14
15Buffet takes a fully-bufferred approach -- all files are fully loaded into
16memory when your app boots, so you will never feel the burn of the filesystem.
17In practice, this is immensely efficient. So much so that putting
18[Varnish](https://www.varnish-cache.org/) in front of your app might even make it
19slower! Well, almost (summary from buffet's `make bench`):
20
21```
22**************** varnish (4874.64 rps)
23*************** buffet-server (4421.13 rps)
24************* buffet (3742.6 rps)
25********* st (2659.29 rps)
26********* node-static (2645.31 rps)
27****** send (1646.75 rps)
28***** ecstatic (1302.24 rps)
29*** paperboy (625.28 rps)
30```
31
32Continuous deployment is also becoming all the rage, and restarting Varnish is
33a pain, so consider using Buffet -- your pages will always be fresh and zesty!
34
35Usage
36-----
37
38### Middleware
39
40Middleware version (compatible with [connect](http://www.senchalabs.org/connect/),
41[union/flatiron](http://flatironjs.org/), [middler](https://npmjs.org/package/middler), etc.)
42
43```javascript
44var connect = require('connect')
45 , app = connect()
46 , buffet = require('buffet')({root: './public'}) // root defaults to ./public
47
48app.use(buffet);
49app.use(buffet.notFound);
50
51var server = require('http').createServer(app);
52server.listen(3000, function () {
53 console.log('test server running on port 3000');
54});
55```
56
57### Easy built-in server
58
59```bash
60$ npm install -g buffet
61$ cd /var/www/html && buffet
62buffet 0.4.0 listening on port 8080
63```
64
65### As a request handler
66
67```javascript
68var server = require('http').createServer();
69var buffet = require('buffet')(); // root defaults to ./public
70
71server.on('request', function (req, res) {
72 buffet(req, res, function () {
73 buffet.notFound(req, res);
74 });
75});
76
77server.listen(3000, function () {
78 console.log('test server running on port 3000');
79});
80```
81
82Options
83-------
84
85- `root`: Document root. Can also be passed as the first parameter to `buffet()`.
86 (Default: `./public`)
87- `indexes`: True to look for `options.index` and serve it for directory requests.
88 (Default: true)
89- `index`: Name of index file to look for. (Default: `index.html`)
90- `gzip`: True to enable gzip when clients can accept it. (Default: `true`)
91- `watch`: True to auto-update the buffer when files change. (Default: `true`)
92- `poweredBy`: True to add the `X-Powered-By` header. (Default: `true`)
93- `maxAge`: Number of max-age seconds to set `Cache-Control` header. Set to
94 `false` or `0` to disable. (Default: `300`)
95- `notFoundPath`: Path to be rendered on `buffetMiddleware.notFound`. (Default:
96 `/404.html`)
97- `keepAlive`: Timeout (in milliseconds) for HTTP keep-alive. (Default: `5000`)
98- `defaultContentType`: If the file does not have an extension, set this to specify the default `Content-Type` sent to the browser. This defaults to `application/octet-stream`.
99
100Running your own benchmark
101--------------------------
102
103Type `make bench` in the buffet directory (you'll need
104[siege](http://www.joedog.org/siege-home/) installed).
105
106Brought to you by [benchmarx](https://github.com/carlos8f/node-benchmarx).
107
108See [here](https://gist.github.com/3473500) for my results.
109
110- - -
111
112### Developed by [Terra Eclipse](http://www.terraeclipse.com)
113Terra Eclipse, Inc. is a nationally recognized political technology and
114strategy firm located in Aptos, CA and Washington, D.C.
115
116- - -
117
118### License: MIT
119
120- Copyright (C) 2012 Carlos Rodriguez (http://s8f.org/)
121- Copyright (C) 2012 Terra Eclipse, Inc. (http://www.terraeclipse.com/)
122
123Permission is hereby granted, free of charge, to any person obtaining a copy
124of this software and associated documentation files (the "Software"), to deal
125in the Software without restriction, including without limitation the rights
126to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
127copies of the Software, and to permit persons to whom the Software is furnished
128to do so, subject to the following conditions:
129
130The above copyright notice and this permission notice shall be included in
131all copies or substantial portions of the Software.
132
133THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
134IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
135FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
136AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
137LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
138OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
139SOFTWARE.
\No newline at end of file