Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

express-promise-middleware

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-promise-middleware

Write express middleware that wait on Promises before they respond or call next

latest
npmnpm
Version
1.0.4
Version published
Maintainers
2
Created
Source

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.';
  );
});

FAQs

Package last updated on 08 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts