Express Middleware Wrapper for Promise-based Chains
raw JSON →The `use-express-middlewares` package provides a utility to adapt traditional Express.js middleware functions, which typically follow the `(req, res, next)` callback pattern, for use within environments that expect middleware to return a Promise. Its core function is to ensure that any wrapped Express middleware yields a Promise, signifying its completion. Released at version 0.1.0, and with its last apparent activity in 2016 (based on the copyright notice), this package is considered abandoned. While it aimed to bridge a gap for asynchronous middleware workflows, the evolution of Node.js and Express.js, particularly with native async/await support and Express 5's improved async middleware handling, has largely superseded the need for such a specific wrapper.
Common errors
error TypeError: wrapMiddleware is not a function ↓
const wrapMiddleware = require('use-express-middlewares');. error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/project/node_modules/some-esm-dependency.js from /path/to/project/node_modules/use-express-middlewares/index.js not supported. ↓
use-express-middlewares, replacing it is the most robust solution. error Request hangs indefinitely / Middleware does not complete ↓
next() (or res.send(), res.end(), etc.) or throws an error. Debug the execution path of the wrapped middleware to ensure its internal promise resolves or rejects under all conditions. Warnings
breaking This package is effectively abandoned, with no updates since 2016. It may contain unaddressed security vulnerabilities, lack compatibility with modern Node.js versions (e.g., Node.js 16+), and is unlikely to receive bug fixes or new features. Consider using native `async` middleware in Express 5 or well-maintained alternatives. ↓
gotcha The package is a CommonJS module. Attempting to `import` it directly in an ES Module (ESM) environment will result in a `SyntaxError` or `ERR_REQUIRE_ESM` unless a CJS-to-ESM wrapper or bundler configuration is used. ↓
gotcha Designed for older versions of Express.js (likely 4.x or earlier). Its behavior and compatibility with Express 5.x, which has improved native support for async middleware, are uncertain and could lead to unexpected issues or redundant wrapping. ↓
Install
npm install use-express-middlewares yarn add use-express-middlewares pnpm add use-express-middlewares Imports
- wrap wrong
import wrap from 'use-express-middlewares';correctconst wrap = require('use-express-middlewares'); - wrapExpressMiddleware wrong
import { wrapExpressMiddleware } from 'use-express-middlewares';correctconst { wrapExpressMiddleware } = require('use-express-middlewares');
Quickstart
const express = require('express');
const wrapMiddleware = require('use-express-middlewares');
const app = express();
const port = 3000;
// A typical Express middleware (callback-based)
const loggerMiddleware = (req, res, next) => {
console.log(`[Express Log] ${req.method} ${req.url} at ${new Date().toISOString()}`);
next(); // Call next to pass control to the next middleware
};
// Wrap the Express middleware to return a Promise
const promiseLoggerMiddleware = wrapMiddleware(loggerMiddleware);
// Simulate a 'use-*' context that expects a Promise
async function processRequest(req, res) {
try {
console.log('--- Starting Request Processing ---');
await promiseLoggerMiddleware(req, res);
console.log('--- Wrapped Middleware Completed ---');
// Continue with other promise-based operations or send response
res.status(200).send('Request processed successfully via wrapped middleware.');
} catch (error) {
console.error('Error in processing:', error);
res.status(500).send('An error occurred.');
}
}
// Example of using the wrapped middleware directly in a route handler
app.get('/', async (req, res) => {
// In a real 'use-*' framework, this would be handled by the framework itself.
// Here, we manually call and await it to demonstrate its promise-returning nature.
await processRequest(req, res);
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
console.log('Access http://localhost:3000/ in your browser.');
});