Reputation: 13
In writing modular JavaScript code for the first time (slowly converting from Java server code to Node JS), I'm having trouble understanding why this code doesn't work.
Node Server code:
const wceTimer = require('./myTimer');
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log('Server running at http://' + hostname + ':' + port + '/');
console.log(typeof wceTimer);
console.log(typeof wceTimer.getTime);
});
Here is my module code:
var myTimer = (function (){
var currentTimeMS = -1;
var timerInterval;
function getTime(){
return currentTime;
}
function getTimeFormatted(){
if(currentTime <= 0) {
return "undefined";
} else {
return new Date(currentTimeMS).toUTCString();
}
}
function advanceTime(){
if(currentTime <= 0) {
this.currentTimeMS = 946684800000;
} else {
this.currentTimeMS = this.currentTimeMS + 1000;
}
}
function startTimer(){
timerInterval = setInterval(advanceTime, 3000);
}
function stopTimer(){
clearInterval(timerInterval);
}
return {
startTimer: startTimer,
stopTimer: stopTimer,
getTime: getTime,
getTimeFormatted: getTimeFormatted
};
})();
Why does line 3 in the server.listen method fail with undefined? Why cannot I use the developer console in my browser to call functions on myTimer?
Upvotes: 0
Views: 537