0

I am implementing a http server for some project.

I have an HttpServer object that I created that contains in it a server (net module)

this server contains allot of info, and i want to pass it as parameter to the callback functions. like you would do with "setTimeout"

var time=setTimeout(function(**a**){do somthing}, 2000, **someObject**);

I tried doing something like that in my code but it does not recognize the parameter I passed as an object

var net = require('net');

function HttpServer(port){
    this.port=port;
}

HttpServer.prototype.start = function (){
    console.log("starting the server");
    this.server = net.createServer(function (socket,server) {
        console.log("my port is: "+server.port)
        socket.on('data',function(dat){  });
                                                 },this);
        //i am trying to send to the createserver callback function
        //the parameter 'this' that actually is an HttpServer
        //and  the callback function secives it as 'server' 
        //when i run the program i get an error that server is
        //undefiend and therefor does not have a member port

    this.server.listen(this.port);
}

var httpserver= new HttpServer(4444);
httpserver.start();

Why it does not recognize the parameter sent?

6
  • If you are trying to pass an object it should not be inside the 'start' function of your web server. You should pass it using GET/POST and then you will get the object/json back in the response. Commented Jan 1, 2012 at 16:51
  • but that is not what i want to do... i the server it self needs that object regardless to what method/request it is given Commented Jan 1, 2012 at 16:53
  • Got it... I think you just got the right answer from @davin. Commented Jan 1, 2012 at 16:57
  • Nah, I didn't actually look at the code, I totally misread the brackets. My answer doesn't do anything... I'm not sure what the OP is trying to do here, but that parameter looks out of place... Commented Jan 1, 2012 at 16:59
  • @user1087995, not sure what you're trying to do, but google bind. You can also achieve similar results with traditional utilisation of the statical scoping of the language. Commented Jan 1, 2012 at 17:03

1 Answer 1

2
var net = require('net');

function HttpServer(port){
    this.port=port;
}

HttpServer.prototype.start = function (){
    console.log("starting the server");
    var that = this;    //Store this to that variable
    this.server = net.createServer(function (socket, server) {
        console.log('Server port is: ' + that.port); // Use that in an anonymous function
        socket.on('data',function(dat){  });
    });
    this.server.listen(this.port);

}


var httpserver= new HttpServer(4444);
httpserver.start();
Sign up to request clarification or add additional context in comments.

1 Comment

I wrote almost the exact answer, but I think you misread the brackets like me. Look closely at the brackets. that and this are in the same scope, so that doesn't change anything... I.e. that isn't actually scoped in the anonymous function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.