@awaitjs/express
Write Express middleware and route handlers using async/await
Usage
const { addAsync } = require('@awaitjs/express');
const { wrap } = require('@awaitjs/express');
API
addAsync()
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.
It adds useAsync()
, getAsync()
, etc. to your Express app
The addAsync()
function adds useAsync()
, deleteAsync()
, getAsync()
,
headAsync()
, paramAsync()
, patchAsync()
, postAsync()
, and putAsync()
.
const express = require('express');
const app = addAsync(express());
app.useAsync(async function(req, res, next) {
await new Promise(resolve => setTimeout(resolve, 50));
});
app.getAsync('*', async function(req, res, next) {
throw new Error('Oops!');
});
app.use(function(error, req, res, next) {
res.send(error.message);
});
const server = await app.listen(3000);
Router()
This module exports a Router()
function that is a drop-in
replacement for express.Router()
, except the returned
router has getAsync()
, useAsync()
, etc.
It exports a Router
function that returns a new async-friendly router
const express = require('express');
const app = express();
const router = Router();
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);
decorateApp
acquit:ignore:end
It is an alias for addAsync()
assert.equal(decorateApp, addAsync)
decorateRouter
It is an alias for addAsync()
assert.equal(decorateRouter, addAsync)
wrap()
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.
It wraps an async function with Express-compatible error handling
const express = require('express');
const app = express();
app.get('*', wrap(async function(req, res, next) {
throw new Error('Oops!');
}));
app.use(wrap(async function(error, req, res, next) {
throw new Error('foo');
}));
app.use(function(error, req, res, next) {
res.send(error.message);
});
const server = await app.listen(3000);