Skip to content

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

@commenthol

Description

@commenthol

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"]

*/

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions