Bogart
A Node.js Web Framework
Inspired by Ruby's Sinatra library, Bogart is a blazing fast, promise-based, "everything-you-wish-sucked-less-about-Express" Node JSGI framework.
Why is Bogart awesome?Features
- Promise-based — No more callback hell
- Response helpers — JSON, HTML, File (Streaming!), CORS (JSON), 302 Redirect, and more
- Template rendering — Server-side templates with partial support
- Middleware support
- Session support
- OAuth support — Facebook and Google+ out of the box
- Static file support — Using the built-in Directory middleware
- Flash — Ruby-esque flash variables
- Parted — A streaming multipart, json, and urlencoded parser
- Imperative response builder — Easily construct your own JSGI responses
- MIT License
Coming soon:
- Bluebird for promises (currently using Q)
- Handlebars as default templating engine (currently using Mustache)
- Twitter OAuth support out of the box
Full documentation is available on GitHub.
Getting Started
Install it with npm: npm install bogart
Then start with the following file:
var bogart = require('bogart');
var router = bogart.router();
router.get('/', function (req) {
return "Hello Bogart";
});
var app = bogart.app();
// A batteries included JSGI stack including streaming request body parsing, session, flash, and much more.
app.use(bogart.batteries);
// Our router
app.use(router);
// Here's lookin' at you, kid
app.start(1337);
Fire up your browser and head to http://localhost:1337.
Routing
Routing follows the major patterns for Sinatra-style frameworks:
router.get('/hello/:name', function (req) {
var greeting = 'Hello ' + req.params.name;
return bogart.html(greeting);
});
Route patterns support wildcards. Wildcards will match anything whereas regular named parameters will not match beyond a path separator ("/").
router.get('/hello/*', function (req) {
return bogart.html('Hello ' + req.params.splat[0]);
});
The router also supports regex:
router.get(/\/posts?/, function () {
// Matches 'post' or 'posts'
return bogart.html('Regex Route');
});
// Regex routes also support splat
router.get(/hello-(.*)/, function (req) {
var name = req.params.splat[0];
return bogart.html('Hello ' + name);
});
Middleware
Middleware logic is executed before a request is passed to a route handler.
Bogart comes with batteries, and you can include them like this:
app.use(bogart.batteries);
// Include the router after the middleware
app.use(router);
app.start();
Writing your own middleware is easy, just create a function that returns the transformation function.
var exampleMiddleware = function (nextApp) {
return function (req) {
// Middleware logic goes here
req.user = {username: 'notduncansmith'};
// Pass the request along when you're done
return nextApp(req);
}
}
// Include it like this
app.use(exampleMiddleware);
JSGI JavaScript Gateway Interface
Bogart sits on top of JSGI, similar to Ruby's Rack or Python's WSGI. Bogart makes it easy to create JSGI Applications and Middleware. Bogart is a high-level wrapper around JSGI.
Valid JSGI responses are always valid returns from Bogart. Bogart, via its helpful Middleware, also allows you to return Node.JS Streams, Node.JS Buffers, and JavaScript Strings as responses that will be translated into valid JSGI responses.
Bogart also provides helper functions to make creating JSGI responses easier. Prime examples of this are bogart.file which returns a JSGI response that serves a file and bogart.proxy which returns a response that proxies another URL.
Contributors
A million thanks to the people that help make Bogart: