Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
express-async-handler
Advanced tools
The express-async-handler package is a middleware for Express.js that simplifies error handling in asynchronous route handlers. It allows you to write asynchronous code without having to manually catch errors and pass them to the next middleware.
Simplified Async Error Handling
This feature allows you to wrap your asynchronous route handlers with the asyncHandler function. If an error occurs, it will automatically be passed to the next middleware, simplifying error handling.
const express = require('express');
const asyncHandler = require('express-async-handler');
const app = express();
app.get('/', asyncHandler(async (req, res, next) => {
const data = await someAsyncFunction();
res.send(data);
}));
app.use((err, req, res, next) => {
res.status(500).send({ error: err.message });
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Integration with Existing Middleware
This feature demonstrates how express-async-handler can be integrated with existing middleware like body parsers. It ensures that any errors in the asynchronous route handlers are properly caught and handled.
const express = require('express');
const asyncHandler = require('express-async-handler');
const app = express();
app.use(express.json());
app.post('/data', asyncHandler(async (req, res, next) => {
const data = await saveData(req.body);
res.status(201).send(data);
}));
app.use((err, req, res, next) => {
res.status(500).send({ error: err.message });
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
async-express is another package that provides similar functionality by allowing you to use async/await in your Express route handlers. It automatically catches errors and passes them to the next middleware. Compared to express-async-handler, async-express offers a similar level of simplicity and ease of use.
express-promise-router is a drop-in replacement for Express's Router that allows you to use promises (including async/await) in your route handlers. It automatically handles rejected promises and passes errors to the next middleware. This package is more comprehensive as it replaces the entire Router, whereas express-async-handler is a middleware function.
Simple middleware for handling exceptions inside of async express routes and passing them to your express error handlers.
npm install --save express-async-handler
or
yarn add express-async-handler
const asyncHandler = require('express-async-handler')
express.get('/', asyncHandler(async (req, res, next) => {
const bar = await foo.findAll();
res.send(bar)
}))
FAQs
Express Error Handler for Async Functions
The npm package express-async-handler receives a total of 141,988 weekly downloads. As such, express-async-handler popularity was classified as popular.
We found that express-async-handler 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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.