Performance-oriented static file server
Serving static files should be the most efficient thing that a Node.js app can do. Turns out, runtime syscalls to the filesystem can really hang your page loads, especially if your filesystem is networked or unreliable in some other way.
Buffet takes a fully-bufferred approach -- all files are fully loaded into
memory when your app boots, so you will never feel the burn of the filesystem.
In practice, this is immensely efficient. So much so that putting
Varnish in front of your app might even make it
slower! Well, almost (summary from buffet's make bench
):
**************** varnish (4874.64 rps)
*************** buffet-server (4421.13 rps)
************* buffet (3742.6 rps)
********* st (2659.29 rps)
********* node-static (2645.31 rps)
****** send (1646.75 rps)
***** ecstatic (1302.24 rps)
*** paperboy (625.28 rps)
Continuous deployment is also becoming all the rage, and restarting Varnish is a pain, so consider using Buffet -- your pages will always be fresh and zesty!
Middleware version (compatible with connect, union/flatiron, middler, etc.)
var connect = require('connect')
, app = connect()
, buffet = require('buffet')({root: './public'}) // root defaults to ./public
app.use(buffet);
app.use(buffet.notFound);
var server = require('http').createServer(app);
server.listen(3000, function () {
console.log('test server running on port 3000');
});
$ npm install -g buffet
$ cd /var/www/html && buffet
buffet 0.4.0 listening on port 8080
var server = require('http').createServer();
var buffet = require('buffet')(); // root defaults to ./public
server.on('request', function (req, res) {
buffet(req, res, function () {
buffet.notFound(req, res);
});
});
server.listen(3000, function () {
console.log('test server running on port 3000');
});
root
: Document root. Can also be passed as the first parameter tobuffet()
. (Default:./public
)indexes
: True to look foroptions.index
and serve it for directory requests. (Default: true)index
: Name of index file to look for. (Default:index.html
)gzip
: True to enable gzip when clients can accept it. (Default:true
)watch
: True to auto-update the buffer when files change. (Default:true
)poweredBy
: True to add theX-Powered-By
header. (Default:true
)maxAge
: Number of max-age seconds to setCache-Control
header. Set tofalse
or0
to disable. (Default:300
)notFoundPath
: Path to be rendered onbuffetMiddleware.notFound
. (Default:/404.html
)keepAlive
: Timeout (in milliseconds) for HTTP keep-alive. (Default:5000
)defaultContentType
: If the file does not have an extension, set this to specify the defaultContent-Type
sent to the browser. This defaults toapplication/octet-stream
.
Type make bench
in the buffet directory (you'll need
siege installed).
Brought to you by benchmarx.
See here for my results.
Developed by Terra Eclipse
Terra Eclipse, Inc. is a nationally recognized political technology and strategy firm located in Aptos, CA and Washington, D.C.
- Copyright (C) 2012 Carlos Rodriguez (http://s8f.org/)
- Copyright (C) 2012 Terra Eclipse, Inc. (http://www.terraeclipse.com/)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.