BackendStack21 / fast-gateway

Fast-gateway is an easy to use Node.js API gateway framework built to handle large scale API traffic with great performance and scalability. Written with JavaScript, it is a framework for the masses!
MIT License
311 stars 35 forks source link

Add Params from Request #46

Closed junaidy-johm closed 4 years ago

junaidy-johm commented 4 years ago

Add Params or Query from Request How can I add params or query from request api gateway to service through the middleware?

I have tried the code below but the new property seen on the console does not appear

'use strict'

const toArray = require('stream-to-array') const gateway = require('../index')

gateway({ routes: [{ prefix: '/httpbin', target: 'https://httpbin.org', hooks: { async onResponse (req, res, stream) { // collect all streams parts const resBuffer = Buffer.concat(await toArray(stream))

    // parse response body, for example: JSON
    const payload = JSON.parse(resBuffer)
    // modify the response, for example adding new properties
    payload.newProperty = 'post-processing'

    // stringify response object again
    const newResponseBody = JSON.stringify(payload)

    // set new content-length header
    res.setHeader('content-length', '' + Buffer.byteLength(newResponseBody))
    // set response statusCode
    res.statusCode = stream.statusCode

    // send new response payload
    res.end(newResponseBody)
  }
}

}] }).start(8080)

jkyberneees commented 4 years ago

Hi @junaidy-johm, please excuse my delay on answering you.
Middlewares at route level are also supported, as you can see in the documentation:

// Optional route level middlewares. Default value: []    
middlewares: []

You can just attach a custom middleware that extends the request object, it will be considered during the proxying.

Please let me know if this works for you, I could also provide you an example if required.

jkyberneees commented 4 years ago

I am closing this issue because of inactivity.