root
A super lightweight web framework with routing and prototype mixin support.
It's available through npm:
npm install root
Usage
Usage is simple
var root = require('root');
var app = root();
app.get('/', function(request, response) {
response.send({hello:'world'});
});
app.post('/echo', function(request, response) {
request.on('json', function(body) {
response.send(body);
});
});
app.listen(8080);
You can extend the request and response with your own methods
app.use('response.time', function() {
this.send({time:this.request.time});
});
app.use('request.time', {getter:true}, function() {
return Date.now();
});
app.get(function(req, res) {
res.time();
});
Routing
Routing is done using murl.
Use the get, post, put, del, patch or options method to specify the HTTP method you want to route
app.get('/hello/{world}', function(req, res) {
res.send({world:req.params.world});
});
app.get('/test', function(req, res, next) {
next();
});
app.get('/test', function(req, res) {
res.send('ok');
});
URL normalization
Before routing an incoming url it is first decoded and normalized
/../../ ⇨ /
/foo/bar/../baz ⇨ /foo/baz
/foo%20bar ⇨ /foo bar
/foo%2fbar ⇨ /foo/bar
This basicly means that you don't need to worry about /.. attacks when serving files or similar.
Error handling
You can specify an error handler for a specific error code by using the error function
app.get('/foo', function(req, res, next) {
res.error(400, 'bad request man');
});
app.error(404, function(req, res) {
res.send({error:'could not find route'});
});
app.error(function(req, res) {
res.send({error:'catch all other errors'});
});
Plugins
To create a plugin simply create a function that accepts an app
var plugin = function(app) {
app.get('/my-plugin', function(req, res) {
res.send('hello from plugin');
});
};
myApp.use(plugin);
Alternatively you can pass a another app instance to use.
var subApp = root();
subApp.get('/test', function(req, res) {
res.send('hello from sub app');
});
myApp.use(subApp);
Available plugins
License
MIT