npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rooster

v1.0.1

Published

A tiny, minimal HTTP router

Downloads

6

Readme

rooster

Code Climate Test Coverage

A tiny tool to route HTTP requests with path parameters.

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

// route parameters are between {curlybraces}
var routes = {
 "GET /": function (req, res) {
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.write("HOME");
    res.end();
 },
 "GET /articles/{article}": function (req, res) {
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.write("you are looking at article: " + req.params.article);
    res.end();
 }
}

// add routes with addRoutes
rooster.addRoutes(routes);

// override the built-in handler for when route doesn't exist
rooster.addDefault(function (req, res) {
  res.writeHead(404);
  res.write("Not found!");
  res.end();
});

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

  // route the request
  rooster.route(req, res);
});

server.listen(4444);

console.log("listening on 4444");

api

rooster exposes 3 very simple methods

addRoutes(routes)

addRoutes is a configuration method for adding routes to rooster

routes - an object of paths and handlers e.g:

var routes = {
  "GET /": function (req, res) {...},
  "GET /articles/{article}": function (req, res) {
    res.writeHead(200);
    res.write(req.params.article);
    res.end();
  }
}

rooster.addRoutes(routes);

Add routes can be called multiple times and, each time, the new routes will be ADDED rather than the old ones being overwritten.

Path parameters are defined within {curly-braces} and are available on the request object through req.params.

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

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

addDefault(handler)

addRoutes is a configuration method for adding routes to rooster

handler(req, res) - a cb for when requests are made to undefined paths

  • handler(req, res) takes 2 arguments:

    • req - the HTTP request object
    • res - the HTTP response object

e.g:

rooster.addDefault(function (req, res) {

  res.writeHead(404);
  res.write("Not found!");
  res.end();
});

route(req, res)

route is the routing method that will match a request against one of the predefined routes

req - the HTTP request object

res - the HTTP request object

e.g:

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

  // route the request
  rooster.route(req, res);
});

note

rooster is build ontop of overalls so check it out for more documentation.

license

MIT