BackendStack21 / restana

Restana is a lightweight and fast Node.js framework for building RESTful APIs.
MIT License
467 stars 27 forks source link

service.use method with multiple handlers only calls the first handler #114

Closed commenthol closed 2 years ago

commenthol commented 2 years ago

When using restana service.use method with multiple handlers but no route only the first handler is called.

If the first argument is a route, then the multiple handlers are considered.

I would expect a similar behavior as with express here. So if the first argument is a handler, then this apples to all routes and methods.

My current workaround is to chain each handler like service.use(first).use(second).use(third). IMHO also service.use(first, second, third) should be supported.

Below you find a sample script which demonstrates the current behavior.

const restana = require('restana')

const service = restana()

service.use((req, res, next) => {
  req.locals = req.locals || []
  next()
})

service.use(
  (req, res, next) => {
    req.locals.push('a1')
    next()
  },
  (req, res, next) => {
    req.locals.push('NIRVANA') /// <<< won't get resolved here!!!
    next()
  }
)

service.use('/',
  (req, res, next) => {
    req.locals.push('b1')
    next()
  },
  (req, res, next) => {
    req.locals.push('b2')
    next()
  }
)

service
  .use((req, res, next) => {
    req.locals.push('c1')
    next()
  })
  .use((req, res, next) => {
    req.locals.push('c2')
    next()
  })

service.get('/', (req, res) => res.send(req.locals))

service.start(3000)

/*

curl "http://localhost:3000/"

gives
  ["a1","b1","b2","c1","c2"]
instead of the expected
  ["a1","NIRVANA","b1","b2","c1","c2"]

*/
jkyberneees commented 2 years ago

Hi @commenthol , thanks for reporting. This is issue is now fixed in restana@4.9.5+

Best Regards

commenthol commented 2 years ago

Many thanks for this great project.