rsnbl

A reasonable web framework for node.js

npm install rsnbl
2 downloads in the last week
2 downloads in the last month

rsnbl

rsnbl is a web framework geared towards helping you organize and re-use your web app's code in a reasonable way.

Installing

Install rsnbl from npm:

npm install rsnbl

a quick example

var rsnbl = require('rsnbl');
var Hello = new rsnbl.Application({
  routes: {
    '/(.*)': function(path) {
      this.http(200, "Hello, "+path);
    }
  }
});
Hello.listen(8080);

Save this code in a file, say example.js, and run it.

$ node example.js
Server listening: localhost:8080

Now check out http://localhost:8080/world in your browser. Try different URLs too, like http://localhost:8080/joe.

a (slightly) more exhaustive example

rsnbl Applications are not much more than a collection of RequestHandler objects, with routes defined to tell rsnbl which handler to invoke on each request. In the "Hello world" example above, the handler was simply a function, which is invoked for every request. We can do more complicated handling by extending rsnbl.RequestHandler.

Extend rsnbl.RequestHandler

Each handler can define any functions or other property it needs to run. The special (and optional) initialize function is called before the handler is given a request (and you must let rsnbl know when it finishes by calling the done function passed to it).

The request and response objects are available through this.request and this.response, respectively.

var MainHandler = rsnbl.RequestHandler.extend({
    initialize: function(done) {
        // do some custom setup here.
        // call done() when you're done, ok?
        done();
    },

    // this function is available as this.doSomethingNeat from other functions
    doSomethingNeat: function() {
      // do something here
      console.log("Doing stuff");
    },

    // rsnbl routes HTTP GET requests to get(), POST to post() etc.
    // the default implementations just end the request with 404 not found
    // HEAD, PUT, and DELETE are also supported, of course (DELETE requests are routed
    // to the function named 'del', if it exists, since delete is a reserved word)

    get: function() {
        // handle request; request and response are found in this.request
        // and this.response, respectively
        this.doSomethingNeat();
    },

    post: function() {
        // ...
    }
});

You can extend your own handlers and override, extend, or just reuse functionality from the parent handler:

var UserHandler = MainHandler.extend({
    get: function(userid) {
        // arguments are filled with any matching regexp groups
        console.log("UserId:", userid);

        // re-use that function in the parent handler
        this.doSomethingNeat();
    }
});

Extend rsnbl.Application

Your application should extend rsnbl.Application, just like request handlers:

var App = rsnbl.Application.extend({
    config: { port: 8888 }, // this is optional

    // also optional, this array of functions is used as middleware
    helpers: [],

    // route string or regex pattern => Handler
    routes: {
      "/users/([a-z0-9]+)": UserHandler
      "/": MainHandler,
      // ...
    }
});

Instantiate your app and call start():

var app = new App();

// start() uses the host/port specified in the config property above
// or, use app.start(port, host), to override the config (required if there was no config)
app.start();
npm loves you