The self.test()
doesn't get triggered inside http.createServer()
:
App.prototype.createServer = function(){
var self = this;
var s = http.createServer(function(req,res){
self.test(); // THIS DOESN`T WORK.
req.addListener('end',function(){
res.writeHead(200,{'Content-Type' : 'application/x-javascript'});
self.test(); // THIS DOESN`T WORK.
});
});
self.test(); // THIS WORKS.
return s;
};
App.prototype.test = function() {
console.log('test')
};
If i move it out of http.createServer()
it works. Why is this?
this
is stored in self
which should make it not lose context. What am i missing here?
I can provide extra code and more functions (self.init
, etc) if necessary.
Instance:
new App({
port: 8000
});
Initialization:
function App(options){
if (! (this instanceof arguments.callee)) {
return new arguments.callee(arguments);
}
var self = this;
self.settings = {
port : options.port
}
self.init();
};
App.prototype.init = function() {
var self = this;
self.server = self.createServer();
self.server.listen(self.settings.port);
};
req
will emit an end
event only if it has a body. GET
requests won't emit it, but POST
will.
Also, you are supposed to call listen to bind your server to a socket. How are you making requests to your server ?