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

rocky-consul

v0.1.1

Published

Rocky HTTP proxy middleware for service discovery and balancing using Consul

Downloads

24

Readme

rocky-consul Build Status NPM

rocky middleware to easily setup a reverse HTTP proxy with service discovery and load balancer using Consul.

Essentially, this middleware will ask to Consul on every interval (configurable) to retrieve a list of URLs of a specific service (e.g: API, CDN, storage), and then them will be provided to rocky in order to balance the incoming HTTP traffic between those URLs.

Installation

npm install rocky-consul --save

Usage

var rocky = require('rocky')
var consul = require('rocky-consul')

var proxy = rocky()

Plug in as global middleware

proxy.use(consul({
  // Servers refresh interval (default to 60000)
  interval: 60 * 5 * 1000,
  // App service name (required)
  service: 'web',
  // Use a custom datacenter (optional)
  datacenter: 'ams2',
  // Consul servers pool
  servers: [
    'http://demo.consul.io',
    'http://demo.consul.io'
  ]
}))

// Handle all the traffic
proxy.all('/*')

proxy.listen(3000)
console.log('Rocky server started')

Plug in as route level middleware

proxy
  .get('/download/:id')
  .use(consul({
    // Servers refresh interval (default to 60000)
    interval: 60 * 5 * 1000,
    // App service name (required)
    service: 'web',
    // Use a custom datacenter (optional)
    datacenter: 'ams2',
    // Consul servers pool
    servers: [
      'http://demo.consul.io',
      'http://demo.consul.io'
    ]
  }))

// Handle the rest of the traffic without using Consul
proxy.all('/*')
  .forward('http://my.server')
  .replay('http://old.server')

proxy.listen(3000)
console.log('Rocky server started')

API

consul(options) => Function(req, res, next)

Return a middleware function with the Consul client as static property function.consul.

Options

  • service string - Consul service. Required
  • servers array<string> - List of Consul servers URLs. Required
  • datacenter string - Custom datacenter to use. If not defined the default one will be used
  • tag string - Use a specific tag for the service
  • defaultServers array<string> - Optional list of default target servers to balance. This avoid asking Consul the first time.
  • protocol string - Transport URI protocol. Default to http
  • timeout number - Consul server timeout in miliseconds. Default to 5000 = 5 seconds
  • interval number - Consul servers update interval in miliseconds. Default to 120000 = 2 minutes
  • headers object - Map of key-value headers to send to Consul
  • auth string - Basic authentication for Consul. E.g: user:p@s$
  • onRequest function - Executes this function before sending a request to Consul server. Passed arguments are: httpOpts
  • onUpdate function - Executes this function on every servers update. Passed arguments are: err, servers
  • onResponse function - Executes this function on every Consul server response. Passed arguments are: err, servers, res

Consul(options)

Internally used micro Consul client interface.

consul#servers(cb)

Returns the Consul servers for the given service. Passed arguments to the callback are: cb(err, servers).

consul#update(cb)

Perform the servers update asking to Consul Passed arguments to the callback are: cb(err, servers).

consul#startInterval()

Start the servers update interval as recurrent job for the given miliseconds defined at options.interval. You should not call this method unless you already called stopInterval().

consul#stopInterval()

Stop server update interval process.

License

MIT - Tomas Aparicio