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

http-service-router

v1.0.2

Published

A tiny ~25L http-router that integrates well with a microservice-like architecture.

Downloads

6

Readme

http-service-router

Standard - JavaScript Style Guide npm version

This is a super tiny router to be used in a microservice-like environment. It parses a given URL and returns a matching object. It also calculates the URL with the part for the match stripped. This is very powerful when being combined with express's path-to-regexp.

Setup

$ npm i http-service-router

Contribute

Pull-requests are always welcome. For any problems or questions just open an issue.

Usage

Simple

The following example shows the simplest use case for the router. Keep reading for more advanced and powerful use-cases:

const createRouter = require('http-service-router')

const router = createRouter([
  [/^\/api/, myhandler]
])

const srv = http.createServer((req, res) => {
  const r = router.match(req.url)
  if (!r) {
    res.writeHead(404, { 'Content-Type': 'text/plain' })
    return res.end('Not found :(')
  }
  r.service(req, res)
})

function myhandler (req, res) {
  // ... Do your magic
}

Advanced use-case

In the following example I'll show you how the router can be utilized to build up a powerful chain of microservices that communicate using a proxy mechanism. This uses the http-proxy module and the expression building module path-to-regexp.

const http = require('http')
const createRouter = require('http-service-router')
const httpProxy = require('http-proxy')
const p = require('path-to-regexp')

const api = httpProxy.createProxyServer({ target: 'http://api' })
const app = httpProxy.createProxyServer({ target: 'http://app' })
const router = createRouter([
  [p('/api', { end: false }), api.web.bind(api)],
  [p('/', { end: false }), app.web.bind(app)]
])
const srv = http.createServer((req, res) => {
  const r = router.match(req.url)
  if (r) {
    // The router also returns a URL where the matching part is stripped. E.g. in the case of the
    // API this would transform '/api/v1/stuff' to '/v1/stuff' and '/api' (or '/api/') to '/'. So
    // the service only receives the URL part that it's interested in.
    req.url = r.url

    // Proxy the request to the matching microservice
    r.service(req, res)
  } else {
    // Send a 404
    res.writeHead(404, { 'Content-Type': 'text/plain' })
    return res.end('Not found :(')    
  }
})

Now api and app can again use the router to dispatch to more nested microservice.

Modular monolith

Sometimes we just don't have the ability to design our application with multiple processes in mind. Maybe we're just starting off and want to deploy the application using a hobby dyno on heroku. But this doesn't disable us to design a modular application which can be easily converted to a microservice-like architecture. I like to use micro to build such applications.

Frontend service (server.js)

const createRouter = require('http-service-router')

const createAuth = require('./auth')
const createAPI = require('./api')
const createApp = /* ... */

module.exports = createFrontend({
  services: {
    api: createAuth({ services: { upstream: createAPI() } }),
    app: createApp()
  }
})

function createFrontend ({ services: { api, app } }) {
  const router = createRouter([
    [p('/api', { end: false }), api],
    [p('/app', { end: false }), app]
  ])

  return async function frontend (req, res) {
    const r = router.match(req.url)
    if (r) {
      req.url = r.url
      return r.service(req, res)
    } else {
      send(res, 404, 'Sorry, this route doesn\'t exist :-(')
    }
  }
}

Auth service (auth.js)

const { createError } = require('micro')

module.exports = createAuth

function createAuth ({ services: { upstream } }) {
  return async function auth (req, res) {
    // ... Do authentication logic (e.g. JSON-Web-Token) ...
    const user = fetchUserSomehow()
    if (!user) throw createError(401, 'Sorry ...')
    req.headers['x-user'] = user,_id
    return upstream(req, res)
  }
}

Api service (api.js)

module.exports = createAPI

function createAPI () {
  return async function api (req, res) {
    // Do API stuff and use `req.headers['x-user']` as user id.
  }
}

This is a nice modular architecture which can be executed using micro server.js. And the best thing is that you can easily convert it to a microservice architecture using the http-proxy approach described above. But it gives you the chance to start-out in a lean fashion without thinking about scaling too much.

Cheers!