Skip to content

An example of policy plugin for express-gateway merging the response of 2 consecutive calls to 2 different services

License

Notifications You must be signed in to change notification settings

ronanquillevere/express-gateway-plugin-merge-example

Repository files navigation

express-gateway-plugin-merge-example

A simple example of plugin policy for express-gateway.

Note that my intention was just to test the plugin system of express-gateway, nothing more.

What this policy does

  • Call http://api.chucknorris.io/jokes/random using superagent
  • Extract the value field of the response body to get the quote (warning some of them can be offensive, I am not responsible for this API)
  • Call http://numbersapi.com/{number} passing the length of the first quote as the number path param
  • Extract the text field of the response body
  • Return javascript object with the 2 quotes to caller, does not call next(), with following format {chuckQuote : quote1, numberQuote: quote2}

flow

Here is what it looks like

screenshot

ES6 and policy code bundling

Code is written in ES6 and bundled using rollup. The config is kind of tricky because of superagent issues. But this is not really the concern of this example !

To bundle (generate) the policy code, simply run

npm run build

This will create the following file dist/merge-example-policy.cjs.js which is the plugin policy code.

I probably could use webpack as well, but I have never used it :)

How to execute it inside express-gateway

Here is my gateway.config.yml

http:
  port: 8080
# https:
#   port: 9999
#   tls: {}
admin: # remove this section to disable admin API
  port: 9876
  hostname: localhost # use 0.0.0.0 to listen on all IPv4 interfaces
apiEndpoints:
  merge:
    host: 'localhost'
    paths: '/merge'
policies:
  - merge-example
pipelines:
  - name: merge
    apiEndpoints:
      - merge
    policies:
        - merge-example :

How I made this work

Thanks to Serhii Kuts

https://groups.google.com/a/express-gateway.io/forum/#!topic/discuss/v0t34Cz-IBU

About

An example of policy plugin for express-gateway merging the response of 2 consecutive calls to 2 different services

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published