Registry / web-framework / exframe-middleware

exframe-middleware

JSON →
library1.5.5jsnpmunverified

The `exframe-middleware` package, currently at version 1.5.5, offers a centralized collection of commonly used middleware functions tailored for both Express.js applications and RabbitMQ RPC services within the broader Exframe ecosystem. Its primary goal is to standardize cross-cutting concerns, such as robust error handling, user authorization, Content Security Policy (CSP) construction, request context building, and consistent API response formatting. Unlike general-purpose middleware libraries, `exframe-middleware` provides specific utilities like `authorizeUser` and `buildExpressRequestContext`, which are designed to integrate seamlessly into an existing Exframe-based microservice architecture. It explicitly uses CommonJS `require()` in its documentation, indicating it was developed with a CJS-first approach. The library promotes consistency and reduces boilerplate across services that require similar operational patterns. Release cadence is not explicitly stated but appears to be stable, with infrequent updates typical for foundational utilities.

npm install exframe-middleware
INSTALL
IMPORT
SIG · EXFRAME-MIDDLEWARE
E
exframe-middleware
web-frameworkjavascriptv1.5.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

handleErrors
import { handleErrors } from 'exframe-middleware';
const handleErrors = require('exframe-middleware').handleErrors;
The package documentation primarily uses CommonJS `require()`. While ESM import syntax may work via bundlers or Node.js's ESM interop, native ESM support is not explicitly guaranteed without a package 'exports' map. `handleErrors` is a function that returns an object containing `express` and `mq` middleware.
authorizeUser
import { authorizeUser } from 'exframe-middleware';
const authorizeUser = require('exframe-middleware');
`authorizeUser` is a named export. Ensure it's imported correctly. It expects `req.user` to be populated by preceding middleware.
standardRouteResponseBuilder
import { standardRouteResponseBuilder } from 'exframe-middleware';
const { default: standardRouteResponseBuilder } = require('exframe-middleware');
`standardRouteResponseBuilder` is a named export, not a default export. It contains utility functions like `singleItem` for formatting responses.

Demonstrates setting up `handleErrors` for Express and applying `authorizeUser` on a protected route, showcasing common middleware patterns within an Express application. It also shows a simulated error and a route demonstrating expected authorization failure.

const express = require('express'); const { handleErrors, authorizeUser } = require('exframe-middleware'); const app = express(); const port = process.env.PORT || 3000; // Apply global error handling middleware app.use(handleErrors().express); // Example route with user authorization app.get('/protected', authorizeUser, (req, res) => { // In a real app, 'req.user' would be populated by a preceding auth middleware. // For this example, we'll simulate a user if one isn't set for authorizeUser to pass. if (!req.user) { req.user = { id: 'test-user', roles: ['admin'] }; } res.json({ message: 'Access granted to protected resource!', user: req.user }); }); // Basic unprotected route app.get('/', (req, res) => { res.send('Hello from Exframe Middleware example!'); }); // Simulate an error to be caught by handleErrors app.get('/error', (req, res, next) => { next(new Error('Something went wrong on purpose!')); }); // If no preceding auth middleware populates req.user, this route will throw UnauthorizedError app.get('/always-fail-auth', authorizeUser, (req, res) => { res.send('This should not be reached.'); }); app.listen(port, () => { console.log(`Exframe Middleware Express app listening on http://localhost:${port}`); });
Debug
Known issues
gotchaThe library is primarily a CommonJS module, with examples explicitly using `require()`. Direct `import` statements in an ES module context may lead to runtime errors (e.g., `ERR_REQUIRE_ESM`) if a default export or 'exports' map is not configured for ESM compatibility. Projects should primarily use `require()`.
fix
Use `const { Name } = require('exframe-middleware');` for all imports. If in an ES module context, consider dynamic `import()` or transpilation.
affects: >=1.0.0
breakingThe package explicitly declares `engines.node: >=14.0.0`. Running on older Node.js versions might lead to unexpected behavior or runtime errors due to reliance on newer language features or APIs.
fix
Ensure your Node.js runtime environment is version 14.0.0 or higher. Use a Node.js version manager like `nvm` to switch versions if necessary.
affects: <14.0.0
gotchaThe `authorizeUser` middleware expects a `user` object to be present on the `request` object (e.g., `req.user`). This implies a preceding authentication middleware (not provided by this package) is responsible for populating `req.user` prior to `authorizeUser` being invoked, otherwise it will always throw an `UnauthorizedError`.
fix
Implement and apply an authentication middleware (e.g., using Passport.js or a custom solution) *before* `authorizeUser` to correctly populate `req.user` with authenticated user data.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined in ES module scope
Attempting to use `require()` syntax in an ES module file (e.g., a `.mjs` file or a file where `"type": "module"` is set in `package.json`).
fix
Either change your project to CommonJS by removing `"type": "module"` from `package.json` and using `.js` extensions, or dynamically `import()` the module if no native ESM entry point is available, e.g., `const { handleErrors } = await import('exframe-middleware');`.
TypeError: handleErrors is not a function
Attempting to use `handleErrors` directly as a middleware function or accessing its properties (like `.express`) without invoking it first.
fix
Ensure `handleErrors` is called as a function to return the object containing the specific middleware. Correct usage is `app.use(handleErrors().express);`.
UnauthorizedError: User not found
The `authorizeUser` middleware was invoked, but `req.user` was `undefined` or falsy, indicating no user object was populated by a preceding authentication layer.
fix
Implement and apply an authentication middleware (e.g., one that verifies a token and sets `req.user`) *before* the `authorizeUser` middleware in your Express route chain.
Upgrade
Version history
1.5.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
exframe-middleware — npm install exframe-middleware · libregistry