1.0.4 • Published 9 years ago

overalls v1.0.4

Weekly downloads
14
License
MIT
Repository
github
Last release
9 years ago

overalls

Code Climate Test Coverage

A small routing tool to decorate HTTP requests with path parameters

use:
var http = require("http");
var overalls = require("overalls");

// parameters are between {curlybraces}
var routes = ["GET /", "GET /dogs/{dog}", "GET /cats/{cat}"];

// overalls is configured with an array of keys
// (or object with paths as keys) i.e { "GET /": function (req, res) {...} }
var decorate = overalls(routes);

var server = http.createServer(function (req, res) {

  //decorate adds request parameters from the matching path to req.params
  decorate(req);

  res.writeHead(200, {"Content-Type": "text/plain"});

  if(req.params) {
    if (req.params.dog) {
      res.write("dogs: " + req.params.dog);
    } else if (req.params.cat) {
      res.write("cat: " + req.params.cat);
    }

  } else {
    res.write("no params");
  }
  res.end();

});

server.listen(4444);

console.log("listening on 4444");

(Also look at /examples for more examples)

api

overalls exposes a single function:

overalls(routes)

routes - an array (or object) of paths. e.g:

var routes = ["GET /", "GET /articles/{article}", "GET /issues/{issue}"];

or

var routes = {
  "GET /": function (req, res) {...},
  "GET /articles/{article}": function (req, res) {...}
}

returns - a decorate function with the signature:decorate(req)

decorate(req)

req - a HTTP request object (although this will work with any object with method and url properties.

Calling decorate on the request object will attempt to match the request path to a path from routes. If there is a matching path, req will be decorated with a params property.

The params property will contain the parameter names as keys.

For example, if decorate() matches the route "GET /articles/{article}" with a GET request to "/articles/13", the request object will look like this:

{
  path: "GET /articles/13",
  params: {
    article: "13"
  },
  ...
}

license

MIT

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago