Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

overalls

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

overalls

Decorate HTTP requests with path parameters

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

Keywords

FAQs

Package last updated on 04 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc