npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

root

v3.2.0

Published

a super lightweight web framework featuring prototype mixin support and routing

Downloads

5,529

Readme

root

A super lightweight web framework with routing and prototype mixin support.

It's available through npm:

npm install root

build status

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:request.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, opts) {
	// opts contains .message which is the message passed to response.error
	// and .stack if an error was passed
	response.send({error:'could not find route'});
});
app.error(function(request, response, opts) {
	response.send({error:'catch all other errors'});
});

Using sub apps

Route requests through an sub app by using app.route

var mobileApp = root();
var myApp = root();
...
myApp.all('/m/*', function(request, response, next) {
	// all routes starting with /m should route through our mobile app as well
	mobileApp.route(request, response, next);
});

As a shortcut you can just pass the app directly

myApp.all('/m/*', mobileApp);

This allows you to easily split up your application into seperate parts and mount them all on one server

Full API

Response

  • response.send(json) will send back json.
  • response.send(string) will send back html (if no Content-Type has been set).
  • response.error(statusCode, messageOrError) send back an error
  • response.redirect(url) send a http redirect

Request

  • request.on('json', listener) will buffer and parse the body as JSON.
  • request.on('form', listener) will buffer and parse the body as a url encoded form
  • request.on('body', listener) will buffer the body as a string
  • request.query contains the parsed querystring from the url

App

  • app.use(methodName, options, fn) extend the request or response with a new prototype method
  • app.(get|put|post|del|options|patch)(pattern, fn) add a route for a http method
  • app.all(pattern, fn) route all methods
  • app.route(request, response, callback) route a request or response from another app
  • app.error(statusCode, fn) add an error handler. use 4xx to match all 400 errors etc.
  • app.on('route', function (request, response) {}) emitted every time a request is being routed
  • app.on('match', function (request, response, pattern) {}) emitted every time a URL pattern is matched

License

MIT