4.0.1 • Published 5 years ago

promisify-express v4.0.1

Weekly downloads
1
License
-
Repository
github
Last release
5 years ago

promisify-express

This package allows you to wrap express so that you can use promises as handlers, and any promise error will be neatly passed to express error handlers.

Getting Started

Install the module with: npm install promisify-express --save.

Documentation

Middleware and route handlers can simply return a promise. If the promise is rejected, promisify-express will call next with the reason. This functionality removes the need to explicitly define a rejection handler.

// Without promisify-express
const express = require('express')
const app = express()

app.get('/url', async (req, res, next) => {
  try {
    throw new Error('something went wrong')
  } catch (e) {
    next(e)
  }
})

app.use((err, req, res, next) => {
  console.error(err)
  res.status(500).send(err.message)
})

// With promisify-express
const express = require('express')
const promisifyExpress = require('promisify-express')
const app = express()
promisifyExpress(app)

app.get('/url', async (req, res, next) => {
  throw new Error('something went wrong')
})

app.use((err, req, res, next) => {
  console.error(err)
  res.status(500).send(err.message)
})