Assuming everything else is correctly setup by you,
I think the cause of the error that you mentioned
TypeError: eventListeners.eventSubscribers is not a function
is that no function is passed to the middleware i.e as a middleware, shouldn't you just pass the eventListeners.eventSubscribers like this
app.use(eventListeners.eventSubscribers);
instead of like this
app.use(eventListeners.eventSubscribers());
If we compare both expressions above, express is not able to pass the request object in the second case as you are calling it but this function call is returning nothing to the express i.e kind of void, but in first case since you passed the function object reference to express, it then calls it as middleware(as a callback later and passes the request object in it while calling it).
Alternatively I think you don't need to use middleware for using the EventEmitter, you can just keep a single global custom instance consisting of EventEmitter as it's property and all the different event you want to listen, and then just use them wherever you require them.You can use the EventEmitter as a property of this custom to emit event in whatever module you want to by importing this custom module there.
This is the working example below
index.js file/entry file
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const routes = require("./userRoute");
//const eventSubs = new eventListeners();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => res.send('App is working'))
app.use('/api', routes);
app.listen(3000, () => console.log('Example app listening on port 3000!'))
This is our global singleton custom instance using EventEmitter as a property.
const { EventEmitter } = require("events");
class CustomEventEmitter {
eventEmitter;
constructor() {
this.eventEmitter = new EventEmitter();
this.initialize();
}
getEventEmitter() {
return this.eventEmitter;
}
initialize() {
this.eventEmitter.on('USER_CREATED', ({ email, name }) => {
console.log('event fired and captured with data', email, name);
});
}
}
module.exports = new CustomEventEmitter();
and here we use our global CustomEventEmitter module in our request routes
i.e userRoute.js
const router = require("express").Router();
const customEventEmitter = require("./CustomEventEmitter");
const createUser = async (req, res, next) => {
var user = req.body
try {
console.log('in controller');
// await newUser(user)
customEventEmitter.getEventEmitter().emit('USER_CREATED', { email: user.email, name: user.name })
res.status(200).send({ success: true });
next()
} catch (e) {
console.log(e.message)
res.sendStatus(500) && next(e)
}
}
router.post("/createUser", createUser)
module.exports = router;