littleware

0.1.3 • Public • Published

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);

Readme

Keywords

none

Package Sidebar

Install

npm i littleware

Weekly Downloads

4

Version

0.1.3

License

none

Last publish

Collaborators

  • kevinmctigue