You haven't yet saved any bookmarks. To bookmark a post, just click <i class='far fa-bookmark'></i>.

Introducing micro-http-router

Hot damn! For the first time, I've uploaded some code that I thought was worth sharing with the world. But first, some background on micro.


micro is a tiny Node.js HTTP API library designed for microservices. In English, that means it's a really small, basic HTTP API that focuses on speed, size, and simplicity.

For another project that I can't yet talk about, the team and I have settled on a microservice architecture for our application. We all have experience with Node and stumbled upon micro by chance and really liked everything about it.

I, however, being spoiled by Express' routing capabilities, saw some room for improvement for micro. (I didn't know at the time that others had already made routers for micro.) Thus, I set forth a few hours of my time and slapped together what I call micro-http-router.

Enter micro-http-router

const micro = require('micro');
const Router = require('micro-http-router');

let router = new Router();

    // Standard GET request
    .get('/', (req, res) => {
        return 'Hello, world';
    // GET request with two route parameters
    .get('/hello/:foo/world/:bar', (req, res) => {
        return `Your first parameter is ${ } and your second parameter is ${ }.`;
    // POST with async body parsing
    .post('/', async (req, res) => {
        const body = await micro.json(req);
        return body;
    // Delete with route parameter
    .delete('/entity/:id', (req, res) => {
        return `Entity with ID ${ } deleted.`;

// Start micro on port 3000
const server = micro((req, res) => router.route(req, res));

The above code block hopefully demonstrates some of the great things about micro-http-router:

  • It's super simple to write: router.[get/post/put/delete/...]('route', (req, res) => ... ) is all it takes
  • It's self-explanatory
  • It's primarily driven by and supports all of the HTTP request methods
  • It only cares about routing, so you still have to handle body parsing as if it were only micro; just make the handler async
  • It's still minimal - 89 SLOC and half of them are only there to aid IntelliSense in VS Code
  • It supports URL parameters like Express


micro-http-router is available on GitHub and as a npm package.


(added 9/4/17)

You know, I have no idea if anyone is reading any of this, but if you're interested, here's what I have planned:

  • Add some tests with ava
  • Switch to a Radix Tree implementation to make routing screaming fast
  • Additional validations
  • Who knows? Shoot me an email at if you have an idea.