express-promisify-router
v1.1.0
Published
Write Express middleware and route handlers using async/await
Downloads
5
Maintainers
Readme
express-promisify-router
Express-promisify-router is a lightweight JavaScript library with zero dependencies that enables async/await support in Express framework applications. With this library, you can write both middleware and routes using async/await syntax. It allows you to return promises directly from route handlers without the need for a try-catch block, simplifying error handling and data transmission to the client.
Installation
npm i express-promisify-router
Usage:
Using Router()
Creates a new async router instance using Router(). This method simplifies the process of creating an async router.
// Usage Example: use wrapped Router()
const { Router } = require('express-promisify-router');
const router = Router();
router.get('/foo', async (req, res, next) => {
const user = await UserService.findById();
if (!user) {
throw new NotFound('User not found');
}
return users;
});
Using wrapRouter()
Wraps an existing Express router instance to add async/await support. This method is useful when you already have an Express router instance and want to add async/await support to it.
// Usage Example: use wrapRouter()
const express = require('express');
const { wrapRouter } = require('express-promisify-router');
const router = wrapRouter(express.Router());
router.get('/foo', async (req, res, next) => {
const user = await UserService.findById();
if (!user) {
throw new NotFound('User not found');
}
return users;
});
Using route()
Provides a more structured way to define routes and their handlers using the route() method. This method allows you to define multiple HTTP methods for a single route.
const { Router } = require('express-promisify-router');
const router = Router();
router
.route('/foo')
.get((req, res, next) => {
return UserService.fetch();
})
.post((req, res, next) => {
return UserService.create();
});
Returning Data Directly
Allows you to return data directly from the route handler without explicitly calling res.send(). This simplifies the process of sending responses to clients.
const express = require('express');
const { wrapRouter } = require('express-promisify-router');
const router = wrapRouter(express.Router());
router.get('/foo', async (req) => {
return await new Promise((resolve) => {
resolve({ message: 'Hello!' });
});
});
Using Array of Middlewares
Enables you to use an array of middleware functions for a single route. Each middleware function in the array will be executed sequentially.
const express = require('express');
const { wrapRouter } = require('express-promisify-router');
const router = wrapRouter(express.Router());
router.get('/foo', [
(req, res, next) => {
next();
},
async (req, res, next) => {
throw new Error('Exception!');
}
]);
Using Classic Middleware Approach
Demonstrates the classic middleware approach for defining route handlers. This method is useful when you need more control over the error handling process.
const express = require('express');
const { wrapRouter } = require('express-promisify-router');
const router = wrapRouter(express.Router());
router.get('/foo', [
(req, res, next) => {
next();
},
(req, res, next) => {
try {
res.send();
} catch (err) {
next(err);
}
}
]);
Support for router.param
RouterWrapper class supports Express's router.param method. This allows you to add middleware functions that will be invoked when a route parameter is matched. For example, you can define a router.param middleware for the userId parameter like this:
router.param(
'userId',
wrapFunction(async (req, res, next, userId) => {
const user = await User.findById(userId);
if (!user) {
const error = new Error('User not found');
error.status = 404;
throw error;
}
req.user = user;
next();
})
);
This middleware will be invoked whenever a route with the :userId parameter is matched. It allows you to perform operations like validating and loading the user data before the route handler is invoked.
API
wrapRouter()
The wrapRouter() function is the best way to add async/await support to your Express app or Router.
wrapFunction()
If you need more control, you can use the wrapFunction() function. This function wraps an async Express middleware and adds async/await support.