Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

default root to ./public, and update examples

  • Loading branch information...
commit b905e5e99c9cb11786dbd89b2ea87ed87215d142 1 parent ba88e60
@carlos8f authored
View
55 README.md
@@ -34,14 +34,6 @@ a pain, so consider using Buffet -- your pages will always be fresh and zesty!
Usage
-----
-### Easy built-in server
-
-```bash
-$ npm install -g buffet
-$ cd /var/www/html && buffet
-buffet 0.4.0 listening on port 8080
-```
-
### Middleware
Middleware version (compatible with [connect](http://www.senchalabs.org/connect/),
@@ -50,18 +42,44 @@ Middleware version (compatible with [connect](http://www.senchalabs.org/connect/
```javascript
var connect = require('connect')
, app = connect()
- , buffet = require('buffet')(root, {options...})
+ , buffet = require('buffet')() // root defaults to ./public
app.use(buffet);
-// also available to serve 404 pages:
app.use(buffet.notFound);
var server = require('http').createServer(app);
+server.listen(3000, function () {
+ console.log('test server running on port 3000');
+});
+```
+
+### Easy built-in server
+
+```bash
+$ npm install -g buffet
+$ cd /var/www/html && buffet
+buffet 0.4.0 listening on port 8080
+```
+
+### As a request handler
+
+```javascript
+var server = require('http').createServer();
+var buffet = require('buffet')(); // root defaults to ./public
+
+server.on('request', buffet);
+server.on('request', buffet.notFound);
+
+server.listen(3000, function () {
+ console.log('test server running on port 3000');
+});
```
Options
-------
+- `root`: Document root. Can also be passed as the first parameter to `buffet()`.
+ (Default: `./public`)
- `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`)
@@ -74,23 +92,6 @@ Options
`/404.html`)
- `keepAlive`: Timeout (in milliseconds) for HTTP keep-alive. (Default: `5000`)
-Example with a raw HTTP server
-------------------------------
-
-```javascript
-var buffet = require('buffet')('/var/www/html', {options...});
-
-http.createServer(function (req, res) {
-
- buffet(req, res, function next () {
- buffet.notFound(req, res);
- });
-
-}).listen(9000, function() {
- console.log('static server running on port 9000');
-});
-```
-
Running your own benchmark
--------------------------
View
10 examples/README.md
@@ -0,0 +1,10 @@
+buffet-examples
+===============
+
+To run the examples,
+
+```bash
+$ npm install
+```
+
+and then `node basic.js` or `node connect.js`
View
9 examples/basic.js
@@ -0,0 +1,9 @@
+var server = require('http').createServer();
+var buffet = require('buffet')(); // root defaults to ./public
+
+server.on('request', buffet);
+server.on('request', buffet.notFound);
+
+server.listen(3000, function () {
+ console.log('test server running on port 3000');
+});
View
11 examples/connect.js
@@ -0,0 +1,11 @@
+var connect = require('connect')
+ , app = connect()
+ , buffet = require('buffet')() // 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');
+});
View
8 examples/package.json
@@ -0,0 +1,8 @@
+{
+ "name": "buffet-examples",
+ "description": "Examples using node-buffet",
+ "dependencies": {
+ "connect": "~2.4.5",
+ "buffet": "~0.4.3"
+ }
+}
View
5 examples/public/index.html
@@ -0,0 +1,5 @@
+<style>
+body { font-family: sans-serif; }
+</style>
+<h1>hello world!</h1>
+<p>this page was served by node-buffet.</p>
View
15 examples/test.js
@@ -1,15 +0,0 @@
-var http = require('http')
- , path = require('path')
- , handler = require('../')(path.resolve(__dirname, '../test/files'))
- , port = 9000
- ;
-
-http.createServer(function(req, res) {
- handler(req, res, function() {
- res.writeHead(404, {'Content-Type': 'text/plain'});
- res.write('file not found');
- res.end();
- });
-}).listen(port, function() {
- console.log('test server running on port 9000');
-});
View
6 lib/index.js
@@ -9,6 +9,10 @@ var url = require('url')
;
module.exports = function buffet(root, opts) {
+ if (typeof root === 'object') {
+ opts = root;
+ root = opts.root;
+ }
var cache = {}, parsed = {}, options = copy(opts);
['indexes', 'gzip', 'watch', 'poweredBy'].forEach(function(opt) {
if (typeof options[opt] === 'undefined') {
@@ -24,8 +28,8 @@ module.exports = function buffet(root, opts) {
if (options.notFoundPath[0] !== '/') {
options.notFoundPath = '/' + options.notFoundPath;
}
+ root || (root = options.root) || (root = './public');
root = path.resolve(root);
- options.root = root;
var ready = false, readyQueue = new EventEmitter;
readyQueue.setMaxListeners(0);
Please sign in to comment.
Something went wrong with that request. Please try again.