Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@awaitjs/express
Advanced tools
Readme
Write Express middleware and route handlers using async/await
const { addAsync } = require('@awaitjs/express');
// Or, if you want to use `wrap()`
const { wrap } = require('@awaitjs/express');
The addAsync()
function is the preferred way to add async/await
support to your Express app. This function adds several helper functions
to your Express app.
useAsync()
, getAsync()
, etc. to your Express appThe addAsync()
function adds useAsync()
, deleteAsync()
, getAsync()
,
headAsync()
, paramAsync()
, patchAsync()
, postAsync()
, and putAsync()
.
const express = require('express');
const app = addAsync(express());
// `useAsync()` is like `app.use()`, but supports async functions
app.useAsync(async function(req, res, next) {
await new Promise(resolve => setTimeout(resolve, 50));
});
// `getAsync()` is like `app.get()`, but supports async functions
app.getAsync('*', async function(req, res, next) {
throw new Error('Oops!');
});
// Because of `getAsync()`, this error handling middleware will run.
// `addAsync()` also enables async error handling middleware.
app.use(function(error, req, res, next) {
res.send(error.message);
});
const server = await app.listen(3000);
This module exports a Router()
function that is a drop-in
replacement for express.Router()
, except the returned
router has getAsync()
, useAsync()
, etc.
Router
function that returns a new async-friendly routerconst express = require('express');
const app = express(); // This app isn't async friendly.
const router = Router(); // But this router is.
router.getAsync('/foo', async function(req, res, next) {
throw new Error('Oops!');
});
app.use(router);
app.use(function(err, req, res, next) {
res.send(err.message);
});
const server = await app.listen(3000);
acquit:ignore:end
assert.equal(decorateApp, addAsync)
assert.equal(decorateRouter, addAsync)
If you need more fine-grained control than what addAsync()
gives
you, you can use the wrap()
function. This function wraps an async
Express middleware or route handler for better error handling.
const express = require('express');
const app = express();
// `wrap()` takes an async middleware or route handler and adds a
// `.catch()` to handle any errors. It also prevents double-calling
// `next()`.
app.get('*', wrap(async function(req, res, next) {
throw new Error('Oops!');
}));
// `wrap()` also supports async error handling middleware.
app.use(wrap(async function(error, req, res, next) {
throw new Error('foo');
}));
app.use(function(error, req, res, next) {
res.send(error.message); // Will send back 'foo'
});
const server = await app.listen(3000);
FAQs
Write Express middleware and route handlers using async/await
The npm package @awaitjs/express receives a total of 5,424 weekly downloads. As such, @awaitjs/express popularity was classified as popular.
We found that @awaitjs/express 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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.