As I work on my expressJS app, I encountered a situation where I needed to share the server object with another file. To achieve this, I decided to create the server in my app.js file and then expose it to one of my routes.
var server = http.createServer(app).listen(4021, function () {
var port = server.address().port;
console.log('http://localhost:%d', port);
});
module.exports = server;
Within the app.js file, I confirmed that the variable holds an object containing relevant server data. However, when attempting to access this variable from my routes.js file using require, I found that it was empty.
var server = require('../../server');
I am puzzled by this issue and would appreciate any insights into what might be going wrong.