a super lightweight web framework featuring prototype mixin support and routing


A super lightweight web framework with routing and prototype mixinsupport.

It's available through npm:

npm install root


Usage is simple

var root = require('root');
var app = root();

app.get('/', function(request, response) {
});'/echo', function(request, response) {
    request.on('json', function(body) {


You can extend the request and response with your own methods

app.use('response.time', function() {
app.use('request.time', {getter:true}, function() {

app.get(function(request, response) {


Routing is done using murl. Use the get, post, put, del, patchor optionsmethod to specify the HTTP method you want to route

app.get('/hello/{world}', function(request, response) {
app.get('/test', function(request, response, next) {
    // call next to call the next matching route
app.get('/test', function(request, response) {

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 errorfunction

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.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.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.querycontains the parsed querystring from the url


  • 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 4xxto 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






  • (源码分析)为什么 Vue 中 template 有且只能一个 root ?

    引言 今年,疫情并没有影响到各种面经的正常出现,可谓是络绎不绝(学不动...)。然后,在前段时间也看到一个这样的关于 Vue 的问题,为什么每个组件 template 中有且只能一个 root? 可能...

    2 个月前
  • 范围和rootscope美元美元之间的差异


    2 年前
  • 深入FiberRoot

    1. 介绍 整个应用的起点 包含应用挂载的目标起点 记录整个应用更新过程的各种信息 2.相关函数及内容 在中我们返回了函数的执行结果, 然就在ReactFiberRoo...

    1 年前
  • 可为什么'T rootscope美元在指令与模板访问隔离范围?

    Nidhish Krishnancamdenkid(提出了一个问题:Why can't ...

    2 年前
  • 为什么我们需要添加“root”

    Tinggo(提出了一个问题:Why we need to add ,或许与您遇到的问题类似。

    2 年前
  • ssl-root-cas

    The module you need to solve node's SSL woes when including a custom certificate. IMPORTANT: Try th...

    3 个月前
  • roots-util

    a utility for building roots extensions Roots Util npm(

    9 个月前
  • roots-cache-manifest

    roots extension that automatically fills in wildcards in a HTML5 cache manifest Roots Cache Manifes...

    9 个月前
  • roots

    simple, flexible, and powerful static site compiler ...

    8 个月前
  • rootpath

    Little helper to make node.js require relative to your project root rootpath ======== Little helpe...

    1 年前


扫码加入 JavaScript 社区