artisan-delegator

Connect based routing middleware

npm install artisan-delegator
6 downloads in the last week
15 downloads in the last month

Artisan Delegator

A URL routing middleware for node.js.

http://artisanofcode.com/projects/artisan-delegator

Example

var http = require('http')
  , delegator = require('artisan-delegator')()
  , widgets = require('./widgets')
  , server
  ;

delegator.add('/widget/{id:number}', {
  GET: function(req, res) {
    var widget = widgets.fetch(req.params.id)
      ;

    if (!widget) {

      res.writeHead(404, {'Content-Type': 'text/plain'});
      res.end('404 Not Found');
    }

    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify(widget));
  }
, PUT: function(req, res) {
    var body = []
      ;

    req.on('data', function (data) {
      body.push(data);
    });

    req.on('end', function () {
      widgets.store(req.params.id, JSON.parse(body.join('')));

      res.writeHead(204);
      res.end();
    });
  }
, DELETE: function(req, res) {
    widgets.delete(req.params.id);

    res.writeHead(204);
    res.end();
  }
});

server = http.createServer(function(req, res) {
    delegator(req, res, function(err) {
      var status = err && err.status || 404
        ;

      res.writeHead(status, {'Content-Type': 'text/plain'});
      res.end('NOT FOUND');
    });
});

server.listen(3000);

Install

To install Artisan Delegator from npm:

npm install artisan-delegator

Source

To install from source:

hg clone https://bitbucket.org/artisanofcode/node-delegator

or

git clone git://github.com/artisanofcode/node-delegator.git

History

1.0.1

Fixing Version Number in Documentation

1.0.0

Initial Release

Licence

This project is licensed under the MIT licence.

Meta

This project uses Semantic Versioning.

npm loves you