I'm having trouble calling a method from within another method in the same class in TypeScript. I'm wanting to call the routeTheRequest
method from inside the onRequest
method, but I can't make it work.
index.ts
import server = require("./server");
new server.MyServer();
server.ts
import http = require("http");
export class MyServer{
public constructor(){
http.createServer(this.onRequest).listen(8888);
console.log("Server has started.");
}
private onRequest(request: http.ServerRequest, response: http.ServerResponse){
var path = request.url;
this.routeTheRequest(path); // *** how do i call routeTheRequest ?? ***
response.writeHead(200, { "Content-Type": "text/plain" });
response.end();
}
private routeTheRequest(urlString: string): void{
console.log("user requested : " + urlString);
}
}
Using this.routeTheRequest
doesn't work, this
loses scope and refers to the http.Server
object that createServer
is returning. I've tried pretty much all the different ways of calling this
from these pages...
How can I preserve lexical scope in TypeScript with a callback function
TypeScript "this" scoping issue when called in jquery callback
A non-jQuery solution would be preferable. Thanks