
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
async-express-router
Advanced tools
An express middleware for wrapping router methods (app.METHOD and express.Router()) with a customizable try / catch
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.
npm install --save async-express-router
or with yarn:
yarn add async-express-router
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.
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
app.get('/path', async (req, res) => {
const results = await someAsyncFunctionThatThrowsAnError()
// This won't execute
res.send(results)
})
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)
})
FAQs
An express middleware for wrapping router methods (app.METHOD and express.Router()) with a customizable try / catch
The npm package async-express-router receives a total of 1 weekly downloads. As such, async-express-router popularity was classified as not popular.
We found that async-express-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.