jkyberneees / fastify-gateway

A Node.js API gateway that just works!
https://www.npmjs.com/package/k-fastify-gateway
MIT License
107 stars 14 forks source link

Not Found #38

Closed InsiderTI closed 5 years ago

InsiderTI commented 5 years ago

Hi,

I dont know if I am doing it right, but when I try to access any route, the gateway always returns 404 (not found). My code is this:

const gateway = require('fastify')({}) gateway.register(require('fastify-reply-from'))

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

middlewares: [ require('cors')(), // https://www.npmjs.com/package/cors require('helmet')() // https://helmetjs.github.io/ ],

routes: [{ prefix: '/gmail', prefixRewrite: '', target: 'https://www.google.com', middlewares: [], hooks: { // onResponse (res, reply) { reply.send(res) } // async onRequest (req, reply) {}, onResponse (res, reply) { reply.send(res) } } }, { prefix: '/admin', prefixRewrite: '', target: 'https://www.google.com', middlewares: [], hooks: { onResponse (res, reply) { reply.send(res) } } }] })

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

jkyberneees commented 5 years ago

Hi @InsiderTI, thanks reaching out.

Proxying gmail I guess will be a bit tricky, but here I share with you an example that you can use as a base:

const fastify = require('fastify')({})
fastify.register(require('fastify-reply-from'))
fastify.register(require('./../index'), {

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

  routes: [{
    prefix: '/httpbin',
    target: 'https://httpbin.org'
  }]
})

fastify.listen(8080).then((address) => {
  console.log(`API Gateway listening on ${address}`)
})

Run this proxy and then call from your browser:

http://localhost:8080/httpbin/get

You should get a proxied response coming from https://httpbin.org

Please let me know if this helps, Regards

jkyberneees commented 5 years ago

Hi Insider TI, now I get your issue, please add the endpoint you want to call on the remote like: http://127.0.0.1:8080/httpbin*/get *

Regards

El lun., 3 jun. 2019 a las 13:37, Insider TI (notifications@github.com) escribió:

Hi jkyberneees. I'm sorry for the delay in replying and thanks for the tip, but not worked. I am still receiving 404 not found. My index.js is now with this code:

const gateway = require('fastify')({}) gateway.register(require('fastify-reply-from'))

gateway.register(require('k-fastify-gateway'), { routes: [{ prefix: '/httpbin', target: 'https://httpbin.org' }],

middlewares: [ require('cors')(), // https://www.npmjs.com/package/cors require('helmet')() // https://helmetjs.github.io/ ] })

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

And the response of http://127.0.0.1:8080/httpbin is: {

"statusCode": 404, "error": "Not Found", "message": "Not Found"

}

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/jkyberneees/fastify-gateway/issues/38?email_source=notifications&email_token=AA7IGXEDY3PA2MXZFR5FEFTPYT7ADA5CNFSM4HREULYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWZEEEI#issuecomment-498221585, or mute the thread https://github.com/notifications/unsubscribe-auth/AA7IGXAICJPFYWRT3PPKW3DPYT7ADANCNFSM4HREULYA .

InsiderTI commented 5 years ago

Hi Insider TI, now I get your issue, please add the endpoint you want to call on the remote like: http://127.0.0.1:8080/httpbin*/get * Regards El lun., 3 jun. 2019 a las 13:37, Insider TI (notifications@github.com) escribió: Hi jkyberneees. I'm sorry for the delay in replying and thanks for the tip, but not worked. I am still receiving 404 not found. My index.js is now with this code: const gateway = require('fastify')({}) gateway.register(require('fastify-reply-from')) gateway.register(require('k-fastify-gateway'), { routes: [{ prefix: '/httpbin', target: 'https://httpbin.org' }], middlewares: [ require('cors')(), // https://www.npmjs.com/package/cors require('helmet')() // https://helmetjs.github.io/ ] }) // start the gateway HTTP server gateway.listen(8080).then((address) => { console.log(API Gateway listening on ${address}) }) And the response of http://127.0.0.1:8080/httpbin is: { "statusCode": 404, "error": "Not Found", "message": "Not Found" } — You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub <#38?email_source=notifications&email_token=AA7IGXEDY3PA2MXZFR5FEFTPYT7ADA5CNFSM4HREULYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWZEEEI#issuecomment-498221585>, or mute the thread https://github.com/notifications/unsubscribe-auth/AA7IGXAICJPFYWRT3PPKW3DPYT7ADANCNFSM4HREULYA .

Sorry, I don't get it. Like this ?

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

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

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

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

  routes: [{
    prefix: '/httpbin',
    prefixRewrite: '',
    target: 'https://httpbin.org/ip get',
    middlewares: [
      require('cors')(),
      require('helmet')()
    ],
    hooks: {
      // async onRequest (req, reply) {},
      // onResponse (req, reply, res) { reply.send(res) }
    }
  }]
})

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

If so, still not working :(

jkyberneees commented 5 years ago

Hi @InsiderTI , please see my example here: https://github.com/jkyberneees/fastify-gateway/issues/38#issuecomment-497620674

routes: [{
    prefix: '/httpbin',
    target: 'https://httpbin.org'
 }]

Means we will route all requests to https://httpbin.org if the url prefix starts with /httpbin. Therefore is you run: http://localhost:8080/httpbin/get the proxy will call the remote on: https://httpbin.org/get.

This is the default behaviour, you can do much more...

Regards

InsiderTI commented 5 years ago

Thanks @jkyberneees, I got it now! Now I understand my error: I am trying to access "http: // localhost: 8080 / httpbin" in the browser, but this route is not part of the proxy routes. Is there a way to intercept the "/" route?

Example: http://localhost:8080/ -> Shows "Hello! This is the gateway!"

jkyberneees commented 5 years ago

Yes, there is a way. I will followup with an ASAP.

El lun., 3 jun. 2019 a las 16:39, Insider TI (notifications@github.com) escribió:

Thanks @jkyberneees https://github.com/jkyberneees, I got it now! Now I understand my error: I am trying to access "http: // localhost: 8080 / httpbin" in the browser, but this route is not part of the proxy routes. Is there a way to intercept the "/" route?

Example: http://localhost:8080/ -> Shows "Hello! This is the gateway!"

Or yet: http://localhost:8080/httpbin -> Shows "Hello! This is another page"

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jkyberneees/fastify-gateway/issues/38?email_source=notifications&email_token=AA7IGXGZO4B6PPARD6NVOXLPYUUIJA5CNFSM4HREULYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWZTRFA#issuecomment-498284692, or mute the thread https://github.com/notifications/unsubscribe-auth/AA7IGXDZLTGL3CFE46TPN4TPYUUIJANCNFSM4HREULYA .

jkyberneees commented 5 years ago

Hi @InsiderTI , I guess this is what you mean:

const fastify = require('fastify')({})
fastify.register(require('fastify-reply-from'))
fastify.register(require('k-fastify-gateway'), {

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

  routes: [{
    prefix: '/httpbin',
    target: 'https://httpbin.org'
  }]
})

fastify.route({
  method: 'GET',
  url: '/',
  handler: function (request, reply) {
    reply.send('Hello! This is the gateway!')
  }
})

fastify.listen(8080).then((address) => {
  console.log(`API Gateway listening on ${address}`)
})

Regards

InsiderTI commented 5 years ago

Hi @InsiderTI , I guess this is what you mean:

const fastify = require('fastify')({})
fastify.register(require('fastify-reply-from'))
fastify.register(require('k-fastify-gateway'), {

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

  routes: [{
    prefix: '/httpbin',
    target: 'https://httpbin.org'
  }]
})

fastify.route({
  method: 'GET',
  url: '/',
  handler: function (request, reply) {
    reply.send('Hello! This is the gateway!')
  }
})

fastify.listen(8080).then((address) => {
  console.log(`API Gateway listening on ${address}`)
})

Regards

Exactly like this @jkyberneees. Thank you!