Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-express-router

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-express-router

An express middleware for wrapping router methods (app.METHOD and express.Router()) with a customizable try / catch

  • 4.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Async Express Router

An express middleware for wrapping router methods (app.METHOD and express.Router()) with a try / catch block. It wrapps every app.METHOD (expect app.use) and express.Router().METHOD that are specified in the express 4.x documentation.

Install

npm install --save async-express-router

or with yarn:

yarn add async-express-router

Usage

const app = require('express')()
const asyncRouter = require('async-express-router')

// Pass in an express app instance
// This has to come before importing any routes
asyncRouter(app)

// Importing routes after asyncRouter(app)
const routes = require('./path/to/routes.js')
app.use(routes)

// All set. Optionally a custom error handler can be set
app.use((err, req, res) => {
  // Do something with it
  res.status(500).send(error.message)
})

app.listen(3000)

Now everything is prepared and all API methods will be wrapped in a try / catch block. This is most useful when working with async / await functions.

Example with express.Router().method
const router = require('express').Router()

router.get('/path', async (req, res) => {
  const results = await someAsyncFunctionThatThrowsAnError()

  // This won't execute
  res.send(results)
})

module.exports = router
Example with app.METHOD
app.get('/path', async (req, res) => {
  const results = await someAsyncFunctionThatThrowsAnError()

  // This won't execute
  res.send(results)
})
Important notice

Even if middlewares are passed in in an array they will be wrapped with the try / catch block if they're holding an async function.

Example

// The bad middleware will fail but it is wrapped with try catch
app.get('/path', [good, good, good, bad, good], (req, res) => {
  res.sendStatus(200)
})

Keywords

FAQs

Package last updated on 14 Jan 2018

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc