I\'m quite new to Node.js / Express, and I\'m using it as a backend for an AngularJS app. I\'ve looked all over StackOverflow for some help on my problem, but I can\'t seem
You can use a pattern based on standard JS closures. The main export in logs.js
will not be the controller function itself, but a factory function that will accept all needed dependencies, and create the controller:
exports.create = function(socket) {
return function(req, res) {
// write body of api request to mongodb
socket.emit();
}
}
Then, when you want to use it:
app.route('/logs').post(logs.create(socket));
Since you set up your routes in a separate package, you have to use the same pattern in routes.js
- routes
should receive the socket to use as a parameter.
This pattern works well if you want to handle those things with DI later, or test your controllers with mock "sockets".