Skip to content

stack-player is a small powerful express middleware that allows for executing middlewares switching between them conditionally and handling their errors separately

Notifications You must be signed in to change notification settings

samislam/stack-player

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stack-player

stack-player is a small, powerful Express middleware that allows for executing middlewares, switching between them conditionally, and handling their errors separately.

For example, you can use stack-player to execute Express middlewares as follows:

userRouter.get('/:userId', stackplayer((req, res, sNext) => {
    /* your code */
}))

userRouter.get('/:userId', stackplayer((req, res, sNext) => (req, res, sNext) => {
    /* your code */
}))

userRouter.get('/:userId', stackplayer((req, res, sNext) => [
    (req, res, sNext) => { /* your code */ },
    (req, res, sNext) => { /* your code */ },
    (req, res, sNext) => { /* your code */ },
], { autoCallNext: true, callNext: true })

userRouter.get('/:userId', stackplayer([
    (req, res, sNext) => { /* your code */ },
    (req, res, sNext) => { /* your code */ },
    (req, res, sNext) => { /* your code */ },
], { autoCallNext: true, callNext: true }))

How this is useful?

This opens up the doors to new express applications design patterns, for example, the following code sample demonstrates using stack-player with fancy-object to set the request within the corresponding middleware stack to the logged in user role:

userRouter.use((req, res, next) => {
  req.$loggedInUser = { role: 'admin' }
  next()
})
userRouter.get(
  '/:userId',
  stackplayer(
    (req) =>
      ({
        superadmin: [(req, res, next) => {}, (req, res, next) => {}],
        admin: [(req, res, next) => {}, (req, res, next) => {}],
        user: [(req, res, next) => {}, (req, res, next) => {}],
      }[req.$loggedInUser.role])
  )
)

About

stack-player is a small powerful express middleware that allows for executing middlewares switching between them conditionally and handling their errors separately

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published