
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
middleware-emitter
Advanced tools
Use middleware to chain your event logic, compatible with express middleware (and possibly others). This allows you to not only break up logic for when an event is fired, but let's you share middleware between frameworks if necessary.
The middleware-emitter project also brings multiple event triggering and capturing.
req The req is the 'request', the req.ctx
is the context on the request, it's used to build up the state throughout the middleware.
res The res is the 'response', the res.ctx
is the context on the response, it's used to build up the output to be used later on.
You can think of the two like this - req = internal (chain only, imagine storing all data for calculation), res = external, when you are at the end of the chain - it's this object that you will want to use for display.
Assuming you have broken up your middleware functions:
const emitter = require('middleware-emitter');
const app = require('./middleware/app');
emitter.on('hello', app.load, app.hello, app.output, app.handleError)
.emit('hello');
$ npm install middleware-emitter
MiddlewareEmitter extends the base EventEmitter class, therefore all standard options apply.
A simple standalone example:
const emitter = require('middleware-emitter');
emitter.on('hello',
(req, res, next) => {
res.ctx.hello = 'world';
next();
},
(req, res) => {
console.log(res.ctx); // { hello: 'world' }
})
.emit('hello');
Listen / emit multiple events:
emitter.on([ 'hello', 'other', 'test' ],
(req, res, next) => {
console.log(req.event.name);
})
.emit([ 'hello', 'other', 'test' ]);
// hello
// other
// test
Inject data into the req (request) context:
emitter.on('inject',
{ hello: 'world' },
(req, res, next) => {
console.log(req.ctx);
})
.emit('inject', { some: 'data' });
// { some: 'data', hello: 'world' }
If you add a function with the 4th parameter of 'err', you can gracefully handle errors:
emitter.on('ohno', (req, res, next) => {
next(new Error('Oh no, something went wrong...'));
},
(req, res, next, err) => {
console.error(err);
next();
},
(req, res) => {
console.log('But we continued anyway.');
})
.emit('ohno');
// Error: Oh no, something went wrong... + stack
// But we continued anyway.
Error handling can be done at any point within the chain, it will automatically hoist the next error handler out for use if an error is passed into a 'next'.
Multiple error handlers can be used, the next error handler in the chain will be used, if there is no next handler, the previous one will be used. If none are found, the error is thrown.
Check out the test folder for more!
From the package
$ npm test
FAQs
EventEmitter with middleware.
We found that middleware-emitter 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
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.