A Node.js API gateway that just work!
Switch branches/tags
Clone or download
Latest commit 1000f06 Nov 20, 2018

README.md

k-fastify-gateway

A Node.js API gateway that just works!

Build Status NPM version Greenkeeper badge

Get started in two steps

Install required dependencies:

npm i fastify fastify-reply-from k-fastify-gateway

Launch your gateway 🔥:

const fastify = require('fastify')({})

// required plugin for HTTP requests proxy
fastify.register(require('fastify-reply-from'))

// gateway plugin
fastify.register(require('k-fastify-gateway'), {

  middlewares: [
    require('cors')()
  ],

  routes: [{
    prefix: '/public',
    prefixRewrite: '',
    target: 'http://localhost:3000',
    middlewares: [],
    hooks: {
      // async onRequest (req, reply) {},
      // onResponse (res, reply) { reply.send(res) }
    }
  }, {
    prefix: '/admin',
    prefixRewrite: '',
    target: 'http://localhost:3001',
    middlewares: [
      require('basic-auth-connect')('admin', 's3cr3t-pass')
    ],
    hooks: {
    }
  }]
})

// start the gateway HTTP server
fastify.listen(8080).then((address) => {
  console.log(`API Gateway listening on ${address}`)
})

Introduction

Node.js API Gateway plugin for the fastify ecosystem, a low footprint implementation that uses the fastify-reply-from HTTP proxy library.

Yeap, this is a super fast gateway implementation!

Motivation

Creating fine grained REST microservices in Node.js is the easy part, difficult is to correctly integrate them as one single solution afterwards!

This gateway implementation is not only a classic HTTP proxy router, it is also a Node.js friendly cross-cutting concerns management solution. You don't have to:

  • repeat in/out middleware logic anymore (cors, authentication, authorization, caching, ...)
  • blame Node.js because the asynchronous post processing of proxied requests was hard to implement...
  • ...
  • or just learn Lua to extend nginx ;)

Configuration options explained

{
  // global middlewares (https://www.fastify.io/docs/latest/Middlewares/)
  middlewares: [],

  // HTTP proxy
  routes: [{
    // Optional path matching regex. Default value: '/*'
    // In order to disable the 'pathRegex' at all, you can use an empty string: ''
    pathRegex: '/*'
    // route prefix
    prefix: '/public',
    // prefix rewrite before request is forwarded
    prefixRewrite: '',
    // remote HTTP server URL to forward the request
    target: 'http://localhost:3000',
    // route level middlewares
    middlewares: [],
    // proxy lifecycle hooks
    hooks: {
      async onRequest (req, reply) {
      //   // we can optionally reply from here if required
      //   reply.send('Hello World!')
      //
      //   return true // truthy value returned will abort the request forwarding
      },
      onResponse (res, reply) {
        // do some post-processing here
        // ...
        // forward response to origin client once finished
        reply.send(res) 
      }

      // other options allowed https://github.com/fastify/fastify-reply-from#replyfromsource-opts
    }
  }]
}

Want to contribute?

This is your repo ;)

Note: We aim to be 100% code coverage, please consider it on your pull requests.