Latest Socket ResearchMalicious Chrome Extension Performs Hidden Affiliate Hijacking.Details
Socket
Book a DemoInstallSign in
Socket

root

Package Overview
Dependencies
Maintainers
1
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

root

a super lightweight web framework featuring prototype mixin support and routing

Source
npmnpm
Version
0.5.15
Version published
Weekly downloads
1.7K
12.55%
Maintainers
1
Weekly downloads
 
Created
Source

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(request, response) {
	response.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(request, response) {
	response.send({world:req.params.world});
});
app.get('/test', function(request, response, next) {
	// call next to call the next matching route
	next();
});
app.get('/test', function(request, response) {
	response.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(request, response) {
	response.error(400, 'bad request man');
});

app.error(404, function(request, response) {
	response.send({error:'could not find route'});
});
app.error(function(req, res) {
	response.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(request, response) {
		response.send('hello from plugin');
	});
};

myApp.use(plugin);

Alternatively you can pass a another app instance to use.

var subApp = root();

subApp.get('/test', function(request, response) {
	response.send('hello from sub app');
});

myApp.use(subApp); // route requests through subApp as well

Available plugins

License

MIT

Keywords

lightweight

FAQs

Package last updated on 19 Sep 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts