Coder Social home page Coder Social logo

anupjha / fastify-gateway Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jkyberneees/fastify-gateway

0.0 2.0 0.0 339 KB

A Node.js API gateway that just work!

Home Page: https://www.npmjs.com/package/k-fastify-gateway

License: MIT License

JavaScript 100.00%

fastify-gateway's Introduction

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: [{
    // 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.