I've created a nodeJS application which needs to create a server which works OK like the following in file server.js:
http.createServer(app).listen(app.get('port'), function (err) {
if (err) {
console.error(err);
} else {
runProcess();
console.log('Server listening on port ' + app.get('port'));
}
});
Now I need to pass this server to some file and my app built like the following:
server.js
app.js
routes.js
action.js
So I did it quick like the following,Is there a better way to do that?
This is the server.js after the changes:
var server = http.createServer(app);
app.setServer(server);
server.createServer(app).listen(app.get('port'), function (err) {
if (err) {
console.error(err);
} else {
runProcess();
console.log('Server listening on port ' + app.get('port'));
}
});
In the app.js I've the router file and I did like the following:
app.use('/', routes.router, function (req, res, next) {
next();
});
app.setServer = function(http) {
routes.setServer(http);
}
module.exports = app;
This code I put in the router.js file:
module.exports = {
router: applicationRouters,
setServer: function(http) {
//here I set server to the file action.js
action.setServer(http);
}
}
And in the action js, I did the following:
module.exports = {
setServer: function(http) {
...
I need to pass the http between layers and not use it as require in different file/module. How do I do that in node.js?