1.0.1 • Published 6 years ago

@tom-julux/httprouter v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

The last HTTP router you will ever need.

Features

  • Universal. Works with any HTTP framework supporting request handlers (ie. everyone)
  • Tiny. Just couple lines of code.
  • Functional. Write your http methods using functions.
  • Async. Design to use with async/await

Usage

Install as project dependency:

$ npm i @tom-julux/httprouter

Use as a request handler:

const { router, get } = require("@tom-julux/httprouter");
const { send } = require("micro");

const hello = (req, res) => res.status(200).send(`Hello ${req.params.who}`);

const notfound = (req, res) => res.status(404).send("Not found route");

const lookup = router(
    get("/hello/:who", hello),
    get("/*", notfound)
);

http
    .createServer((req, res) => lookup(req, res))
    .listen();

route methods

Each route is either a single basic or any http method that you import from @tom-julux/httprouter and has the same arguments:

  • get(path = String, handler = Function)
  • post(path = String, handler = Function)
  • put(path = String, handler = Function)
  • patch(path = String, handler = Function)
  • del(path = String, handler = Function)
  • head(path = String, handler = Function)
  • options(path = String, handler = Function)
  • any(path = String, handler = Function)

path

A simple url pattern that you can define your path. In this path, you can set your parameters using a : notation. The req parameter from handler will return these parameters as an object.

For more information about how you can define your path, see url-pattern that's the package that we're using to match paths.

handler

The handler method is a simple function that will make some action base on your path. The format of this function is async (req, res) => {}

UrlPattern instance as path

The package url-pattern has a lot of options inside it to match url. If you have a different need for some of your paths, like a make pattern from a regexp, you can pass an instance of UrlPattern as the path parameter:

const UrlPattern = require("url-pattern");
const { router, get } = require("@tom-julux/httprouter");

const routes = router(
    get(new UrlPattern(/^\api/), () => 'This will match all routes that start with "api"')
);

Namespaced Routes

If you want to create nested routes, you can define a namespace for your routes using the withNamespace high order function:

const { withNamespace, router, get } = require("@tom-julus/router");
const { json, send } = require("micro");

const oldApi = withNamespace("/api/v1");
const newApi = withNamespace("/api/v2");

const routes = router(
    oldApi(get("/", () => "My legacy api route")),
    newApi(get("/", () => "My new api route"))
);

Nested Namespaces

To make create a nested namespace yout can use the routerNamespace function.

const { routerNamespace, withNamespace, router, get } = require("@tom-julus/router");

const fooRoutes = withNamespace("/foo");

const innerBar = routerNamespace(
    get("/", () => "inner bar")
);

const innerFoo = routerNamespace(
    get("/", () => "inner foo"),
    innerBar("/bar")
);

const routes = router(
    fooRoutes(
        get("/", () => "foo"),
        innerFoo("/bar")
    )
);

Thanks

This project is heavily based on the works of Pedro Nauck on micro-router, which is sadly no longer in development. It also incoperates the routerNamespace (nested Namespaces) PR.

1.0.1

6 years ago

1.0.0

6 years ago