
Security News
Socket Security Analysis Is Now One Click Away on npm
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.
@root/async-router
Advanced tools
A lightweight, zero-dependency JavaScript library to bring native Promises and
async/await to Express.
Wraps the Express Router and provides a drop-in replacement to allow you to progressively enhance your routes with Promise and await support.
// Handle Async & Promise routes - and normal routes too!
app.get('/foo', async function (req, res) {
let user = await UserService.findById();
if (!user) {
throw new Error('User not found');
}
// res.json() will be called automatically
return users;
});
express.Router()
Promisesasync/awaitres.json() can be called automatically'use strict';
let http = require('http');
let express = require('express');
let app = require('@root/async-router').Router();
// Handle Async & Promise routes
app.get('/foo', async function (req, res) {
let user = await UserService.findById();
if (!user) {
throw new Error('User not found');
}
// res.json() will be called automatically
return users;
});
// Handles existing routes too - no refactoring required!
app.get('/foo', async function (req, res) {
try {
let user = await UserService.findById();
} catch (e) {
console.error('Unexpected');
console.error(e);
res.statusCode = 500;
res.end('Internal Server Error');
}
if (!user) {
res.statusCode = 404;
res.json({ error: 'User not found' });
return;
}
res.json(users);
});
// Handle errors (must come after associated routes)
app.use('/', function (err, req, res, next) {
console.error('Unhandled Error');
console.error(err);
res.statusCode = 500;
res.end(err.message);
});
// Start node.js express server
let server = express().use('/', app);
http.createServer(server).listen(3000, function () {
console.info('Listening on', this.address());
});
let app = require('@root/async-router').Router();
This is just a wrapper around express.Router(), which is what provides the
default router and "mini apps" of express - so it has all of the same methods
and function signatures:
app.use(path, middlewares);
app.route(path, minApp);
app.head(path, fns);
app.get(path, fns);
app.post(path, fns);
app.patch(path, fns);
app.delete(path, fns);
// ... etc
Any incompatibility should be file as a bug.
It does NOT copy the top-level express server API. You should still use express for that:
let server = express()
// top-level server options
.set('trust proxy', 1)
// set async router
.use('/', app);
require('http')
.createServer(server)
.listen(3000, function () {
console.info('Listening on', this.address());
});
The wrap(app) is the best way to add async/await
support to your Express app or Router.
let syncApp = express.Router();
let app = require('@root/async-router').wrap(syncApp);
Fork of express-promisify-router to bugfix error handling.
MIT License
See LICENSE.
FAQs
Write Express middleware and route handlers using async/await
The npm package @root/async-router receives a total of 194 weekly downloads. As such, @root/async-router popularity was classified as not popular.
We found that @root/async-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
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.

Security News
A compromised npm publish token was used to push a malicious postinstall script in cline@2.3.0, affecting the popular AI coding agent CLI with 90k weekly downloads.

Product
Socket is now scanning AI agent skills across multiple languages and ecosystems, detecting malicious behavior before developers install, starting with skills.sh's 60,000+ skills.