moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
294 stars 118 forks source link

Multi route does not work properly #64

Closed benjsicam closed 6 years ago

benjsicam commented 6 years ago

Code to reproduce:

const { ServiceBroker } = require('moleculer')
const ApiService = require('moleculer-web')

const broker = new ServiceBroker()

broker.createService({
  name: 'api-gateway',
  mixins: [
    ApiService
  ],
  settings: {
    routes: [{
      path: '/',
      mappingPolicy: 'restrict',
      aliases: {
        'GET /': '$node.health'
      }
    }, {
      path: '/healthz',
      mappingPolicy: 'restrict',
      aliases: {
        'GET /': '$node.health'
      }
    }]
  }
})

broker.start()

Expected Result:

Both GET / and GET /healthz should the node health response

Actual Result:

GET / will work and respond with the node health GET /healthz returns an HTTP 404 Error

benjsicam commented 6 years ago

Ok. I think it has something to do with how the routes are ordered upon declaration. This fixed it for me so anyone who bumps into this issue shouldn't spend lots of time debugging this.

const { ServiceBroker } = require('moleculer')
const ApiService = require('moleculer-web')

const broker = new ServiceBroker()

broker.createService({
  name: 'api-gateway',
  mixins: [
    ApiService
  ],
  settings: {
    routes: [{
      path: '/healthz',
      mappingPolicy: 'restrict',
      aliases: {
        'GET /': '$node.health'
      }
    }, {
      path: '/',
      mappingPolicy: 'restrict',
      aliases: {
        'GET /': '$node.health'
      }
    }]
  }
})

broker.start()

Declare the root last.

icebob commented 6 years ago

Yes, I will add this info into docs