I'm writing an http server in node.js.
The Server object has several fields that should be sent to the client upon request. This is why I need to pass status() to router.route() - so it can be called from within (after requests are parsed) and return updates variable values. The problem is that when status() is called it does not print the fields values, but rather the object literal.
The constructor Server is as follows:
this.server = net.createServer(connectionHandler);
this.resourceMap = resourceMap;
this.rootFolder = rootFolder;
this.isStarted = false;
this.startedDate = undefined;
this.port = undefined;
this.numOfCurrentRequests = 0;
function status() {
return {
"isStarted" : this.isStarted,
"startedDate" : this.startedDate,
"port" : this.port,
"resourceMap" : this.resourceMap,
};
}
function connectionHandler(socket) {
console.log('server connected');
console.log('CONNECTED: ' + socket.remoteAddress +':'+ socket.remotePort);
socket.setEncoding('utf8');
socket.on('data',function(data) {
this.numOfCurrentRequests += 1;
router.route(status,data,socket,handle,resourceMap,rootFolder);
});
}
this.startServer = function(port) {
this.port = port;
this.isStarted = true;
this.startedDate = new Date().toString();
this.server.listen(port, function() {
console.log('Server bound');
});
}
}
And when status is invoked from within router.route() I get
function status() {
return {
"isStarted" : this.isStarted,
"startedDate" : this.startedDate,
"port" : this.port,
"resourceMap" : this.resourceMap,
};
}
The way I understand it functions are variables and therefore passed by value. Any way I can solve my problem?
Thanks
If I have clearly reach you, you don't want a function pointer but it's result. So status should be passed as the follows:
router.route(status(),data,socket,handle,resourceMap,rootFolder);
Eventually the following object will be passed:
return {
"isStarted" : this.isStarted,
"startedDate" : this.startedDate,
"port" : this.port,
"resourceMap" : this.resourceMap,
}
As you want to display them, on your callback use the following code
for(var s in status) {
console.log(s+" : "+status[s]);
}
For those like me who actually meant "function pointer" in their Google search, here is an answer :
Admit we require an external file in our app.js file :
var _commandLineInterface("./lib/CommandLine")();
var _httpServer = require("./lib/HttpServer")(_commandLineInterface);
And then, in our HttpServer.js file, admit we want to use the _commandLineInterface object passed as an argument of _httpServer constructor. We will do :
function HttpServer(_cli){
console.log(_cli);
}
module.exports = HttpServer;
BZZZZZZZZT ! Error. The _cli pointer appears to be undefined. It's over. Everything is lost.
Okay... Here is the trick : Remember our CommandLine object ?
function CommandLine(){
...
return this;
}
module.exports = CommandLine;
Yep. That's pretty weird when you're not used to nodejs behaviour.
You'll have to say to your object that it has to return itself after his construction. And for someone used to front-end Javascript behaviour, that's not normal.
So, when you add the little "return trick", you will be able to get your pointer from inside of your other object :
function HttpServer(_cli){
console.log(_cli); // show -> {Object}
}
Hope it will help some nodejs newbies like me.