Skip to content

Commit

Permalink
Docs and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Rodriguez committed Jul 22, 2012
1 parent 20a99dc commit 6db3d66
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 7 deletions.
76 changes: 73 additions & 3 deletions README.md
@@ -1,4 +1,74 @@
node-buffet
===========
buffet
------

Performance-oriented static file server
Performance-oriented static file server

Idea
====

Buffet was borne out of frustration. 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 you're getting that
humongous burst of traffic you've been dreaming of, and your filesystem is
networked or unreliable in some other way.

Buffet takes a fully-bufferred approach (hence the name, hehe) -- 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](https://varnish-cache.org/) in front of your app might even make it
slower!

Continuous deployment is also becoming all the rage, and restarting Varnish is
a pain, so consider using Buffet instead, so your pages are always fresh and
zesty.

Usage
=====

Easy built-in server:

```bash
$ npm install -g buffet
$ cd /var/www/html
$ buffet
buffet 0.2.3 listening on port 8080
```

Middleware version (compatible with [connect](http://www.senchalabs.org/connect/),
[union/flatiron](http://flatironjs.org/), etc.

```javascript
var buffetMiddlware = buffet(root, {options...});
// also available to serve 404 pages:
buffetMiddleware.notFound(req, res);
```

Options
=======

- `indexes`: True to look for `options.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 the `X-Powered-By` header. (Default: `true`)
- `maxAge`: Number of max-age seconds to set `Cache-Control` header. Set to
`false` or `0` to disable. (Default: `300`)
- `notFoundPath`: Path to be rendered on `buffetMiddleware.notFound`. (Default:
`/404.html`)

Example
=======

```javascript
var buffet = require('buffet')('/var/www/html', {maxAge: 86400})
, http = require('http')
, port = 9000
;

http.createServer(function(req, res) {
buffet(req, res, buffet.notFound.bind(null, req, res));
}).listen(port, function() {
console.log('test server running on port 9000');
});
```
23 changes: 20 additions & 3 deletions bin/buffet.js
Expand Up @@ -5,11 +5,11 @@ var argv = require('optimist')
.alias('p', 'port')
.default('p', 8080)
.alias('l', 'log')
.default('l', true)
.alias('v', 'version')
.argv
, http = require('http')
, accesslog = require('accesslog')
, buffet = require('../')(argv.root, {watch: argv['watch'], maxAge: argv['max-age'], notFoundPath: argv['404']})
, version = require(require('path').join(__dirname, '../package.json')).version
;

Expand All @@ -19,11 +19,27 @@ if (argv.v) {
}
else if (argv.help) {
console.log('Usage: buffet '
+ '[--root=dir] [-p port | --port=port] [--log | --log=file...]\n'
+ ' [--no-watch] [--conf=file...] [--max-age=seconds]');
+ '[--root=dir] [--port=port] [--no-log | --log=file...] [--no-watch]\n'
+ ' [--conf=file...] [--max-age=seconds] [--404=404.html]\n'
+ ' [--no-indexes] [--index=index.html]');
process.exit();
}

var options;
if (argv.conf) {
options = require(argv.conf);
}
else {
var options = {
root: argv['root']
watch: argv['watch'],
maxAge: argv['max-age'],
notFoundPath: argv['404']},
indexes: argv['indexes'],
index: argv['index']
};
}
var logger;
if (argv.log) {
var loggerOptions = {};
Expand All @@ -39,6 +55,7 @@ else {
};
}
var buffet = require('../')(options.root, options);
http.createServer(function(req, res) {
logger(req, res, function() {
buffet(req, res, buffet.notFound.bind(null, req, res));
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Expand Up @@ -17,6 +17,7 @@ module.exports = function buffet(root, opts) {
}
});
options.maxAge || (options.maxAge = 300);
options.index || (options.index = 'index.html');
options.notFoundPath || (options.notFoundPath = '/404.html');
if (options.notFoundPath[0] !== '/') {
options.notFoundPath = '/' + options.notFoundPath;
Expand Down Expand Up @@ -92,7 +93,7 @@ module.exports = function buffet(root, opts) {
}

function isIndex(urlPath) {
return options.indexes && path.basename(urlPath) === 'index.html';
return options.indexes && path.basename(urlPath) === options.index;
}

function onReady(cb) {
Expand Down

0 comments on commit 6db3d66

Please sign in to comment.