Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A small, speedy router with a bonus middleware framework for free
JavaScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
README.md
littleware.js
package.json

README.md

Littleware

A small, speedy router with a bonus middleware framework for free.

Littleware is based off of Simpleware, with several features and performance improvements added.

Router

The Router class is a small, high-performace router that can be used as middleware or by itself.

Uage:

var http = require('http'),
    Router = require('littleware').Router,
    router = new Router();

router.get('/', indexHandler); // Shortcuts for common methods
router.post('/', indexPostHandler);
router.route('head', '/', cacheCheckHandler); // Or use any method

http.createServer(router.mw()).listen(80);

Express-style param matching

router.get('/:name/:paramTwo?', function(req, res) {
  res.write('Your name: ' + req.param('name'));
});

// Regex matches also work
router.get(/^\/api\/([a-z]+)/, function(req, res) {
  console.log('Called api method: ' + req.param(0));
});

// req.path and req.query are parsed from req.url
app.get('/queryendpoint', function(req, res) {
    console.log('Called ' + req.path + 'with API key ' + req.query.key);
    console.log('Whole request url was ' + req.url);
});

Multiple route handlers

// Any handlers that could be put into mw also work here
app.get('/authed', cookieParser, getUser, authedHandler);

Middleware

mw takes any number of arguments. Each argument must be a handler, or an array of handlers. Handlers will be run sequentially until one does not call next.

Using the middleware functionality:

var http = require('http'),
    mw = require('littleware').mw;

var middlewareKit = [
  cookieParser,
    function(req, res, next) {
        req.user = getCurrentUser(req);
        next();
    }
];

var app = mw(middlewareKit, function(req, res) {
    console.log();
});

http.createServer(app).listen(80);

Router and mw can be used in combination:

var http = require('http'),
    littleware = require('littleware'),
    Router = littleware.Router,
    mw = littleware.mw;

var router = new Router();

router.get('/', indexHandler);

http.createServer(
  mw(
      staticFileServer,
      router.mw(),
      notFoundHandler
  )
).listen(80);
Something went wrong with that request. Please try again.