BackendStack21/fast-gateway

Problem configuring a prefix with /

Closed this issue · 3 comments

Hello,
It seems that configuring a / does not work well.
I have following typical code:
const gateway = require('fast-gateway')
const server = gateway({
routes: [{
prefix: '/',
target: 'http://localhost:81'
},{
prefix: '/data',
target: 'http://localhost:5000',
middlewares: [(req, res, next) => {
req.cacheDisabled = true
return next()
}]
}]
})
server.start(80)

The forwarding of the prfix /data works well but I acn`t use the prefix /.
It will not respond correctly . I get following errors:
Request URL: http://dhcom/styles.9486fc057232f4cb5d97.css
Request Method: GET
Status Code: 404 Not Found
Remote Address: 192.168.178.101:80
Referrer Policy: no-referrer-when-downgrade
Connection: keep-alive
Content-Length: 0
Date: Sat, 21 Mar 2020 14:10:31 GMT
Accept: text/css,/;q=0.1
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control: no-cache
Connection: keep-alive
Host: dhcom
Pragma: no-cache
Referer: http://dhcom/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36

the file styles.9486fc057232f4cb5d97.css is there but I get the error 404 back.

Could you help me to solve this problem by using the prefix / ?

Same question, here. I've got an existing server on the URL, and I'd like to ensure that all the existing links don't change.

I can put my existing server behind the prefix /app and it works great...except that now all my existing users must now use /app, which won't meet my requirements. So I need to route / to the "legacy" server, while creating a new prefix /v2 or similar to target the new server.

Example: With this gist, I'd expect navigating to http://localhost:8080/hi to respond with "Hello World!" but I get a 404.

https://gist.github.com/nealtovsen/410a347503723e5245db56cbffcf87cf

Hi @tangueroR and @nealtovsen, excuses for taking so long to answer this questions.

As you can imagine the problem stays in the current implementation using a regular expression matcher that does not consider /, instead /* routes. As prefixes act as discriminators, adding a global /* matching expression is required to be defined at last.

In order to still support a default / routing, I would recommend to configure a wildcard route configuration.

For example:

const server = gateway({
  routes: [{
    prefix: '/v2',
    target: 'http://localhost:3000'
  },{
    prefix: '/*',
    pathRegex: '',
    target: 'http://localhost:3001'
  }]
})

Looking forward to your feedback,
Best Regards,
Rolando

Thanks @jkyberneees ! That seems to work well. Unfortunately I only now noticed that websockets are not supported yet, so fast-gateway will still not bet the answer for my current needs. But looks like a good library, otherwise.