npm install middleware-bind
|1||downloads in the last month|
|Version||1.0.0 last updated 2 years ago|
Transforms an asynchronous function into a middleware.
The result of the function will be bound to
Use this library if you want to load async data in a clean way.
Transforms an asynchronous function
f into a middleware.
The middleware will call
f has to call
which will cause
value to be bound to
bind name, (req, next) -> # do async stuff and call `next` when you are finished error = null result = 'result' next error, result
name is the directive where to save the results from
# will bind the result of asnycMethod to `req.loaded.foo` bind 'foo', (req, next) -> asyncMethod next
name can be nested.
# will bind the result of asyncMethod to `req.loaded.foo.bar` bind 'foo.bar', (req, next) -> asyncMethod next
If the asynchronous method returns an error the process is aborted and a Status Code of 500 is returned to the user.
router.get '/user', bind 'users.all', (req, next) -> dao.user.all next (req, res, next) -> console.log 'loaded users', req.loaded.users.all # will output the result of `dao.user.all` next() render view.user.all router.get '/user/:id', bind 'user.object', (req, next) -> dao.user.byId req.params.id, next render view.user.show
bind was developed with snd during a project.
The initial idea was from snd.
I refactored the library and provided unit tests.