express-promise-middleware

1.0.4 • Public • Published

express-promise-middleware

This module exports two handy functions that let you write express middleware that wait on Promises before they respond or call next. Nothing fancy really. Comes with flowtypes if you use those.

promiseMiddleware

Returns express middleware that runs provided function, and when complete, calls next() for you.

If there is an error, it catches the error and passes it along to next() like a good express citizen.

Also, the provided middleware returns a promise that is resolved whenever the middleware is done. Why not? It's helpful for tests. Even if the handler throws, though, the promise will resolve without an error so you don't have to deal with uncaught exceptions.

import {promiseMiddleware} from 'express-promise-middleware';
 
router.use(promiseMiddleware(function (req, res) {
  return Promise.resolve()
  .then(function () {
    /*
     * do something async, the next middleware will wait
     * 'till this one is done
     */
  });
}));

respondJSON

Returns express middleware that runs provided function, expects a promise back, and uses the express res.json() function to send along the response.

If there is an error, it catches the error and passes it along to next() like a good express citizen.

Also, the provided middleware returns a promise that is resolved whenever the middleware is done. Why not? It's helpful for tests. Even if the handler throws, though, the promise will resolve without an error so you don't have to deal with uncaught exceptions.

import {respondJson} from 'express-promise-middleware';
 
router.get('/my-endpoint', respondJson(function (req, res) {
  res.setStatus(201);
  return Promise.resolve(
    'This is a string response object! It can be ' +
    'whatever you want, so long as it\'s valid JSON!' +
    'When this promise resolves, express will respond ' +
    'to the request.';
  );
});

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.4
    6
    • latest

Version History

Package Sidebar

Install

npm i express-promise-middleware

Weekly Downloads

6

Version

1.0.4

License

MIT

Unpacked Size

27.5 kB

Total Files

7

Last publish

Collaborators

  • goodeggs-admin
  • demands