Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

156 lines (121 sloc) 4.057 kB

middler

An embeddable middleware runner

Build Status

Idea

middler is a flexible, tiny middleware runner for Node.js which can easily be embedded in an existing http server or even an existing middleware chain. Also provided is express-like routing and union compatibility. Best of all, the code is dead simple and dependency-free.

Enjoy!

Install

$ npm install middler

Basic usage

var middler = require('middler')
  , server = require('http').createServer()
  , buffet = require('buffet')('./public')

middler(server)
  .add(buffet)
  .add(function(req, res, next) {
    // ... do some stuff
    next();
  })
  .add(buffet.notFound);

server.listen(3000);

Routing

middler(server)
  .get('/robots.txt', function (req, res, next) {
    res.end('humans only!');
  })
  .post('/posts/:id', function (req, res, next) {
    // req.params.id available
  });

Multiple paths/methods/handlers

middler(server)
  .add(['get', 'post'], '/', function (req, res, next) {
    // handle both get and post requests
  });

// Or add multiple handlers
function bodyParser (req, res, next) { req.body = ... }
function formHandler (req, res, next) { use req.body ... }

middler(server)
  .post('/posts', [bodyParser, formHandler]);

middler is a middleware, too

Writing some middleware and need routes? Just use middler().handler which returns a middleware handler!

var connect = require('connect')
  , http = require('http')
  , middler = require('middler')

var app = connect()
  .use(connect.favicon())
  .use(connect.logger('dev'))
  .use(connect.static('public'))
  .use(connect.directory('public'))
  .use(connect.cookieParser('my secret here'))
  .use(connect.session())
  .use(middler()
    .get('/', function (req, res, next) {
      res.end('hello world!');
    })
    .add(function (req, res) {
      res.writeHead(404);
      res.end('page not found');
    })
    .handler
  );

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

union compatibility

middler(server)
  .add(function () {
    // this.req
    // this.res
    this.res.emit('next');
  });

Benchmarks

To run benchmarks vs. competing middleware, type make bench in the middler root.

License

Developed by Terra Eclipse

Terra Eclipse, Inc. is a nationally recognized political technology and strategy firm located in Aptos, CA and Washington, D.C.

http://www.terraeclipse.com

License: MIT

Copyright (C) 2012 Terra Eclipse, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Jump to Line
Something went wrong with that request. Please try again.