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
You can fix this by writing the onRequest method like this. Note, you shouldn't write all methods like this as it does cost some performance (it creates a new function for every instance instead of putting it on the prototype) but in this case that doesn't really matter.
class MyServer {
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();
}
}
Notice the lambda on the function.
Another way is writing:
http.createServer(this.onRequest.bind(this)).listen(8888);
edit:
I talked about that it does cost performance but the impact isn't that big, but what I actually meant by that was don't write every method this way (only the ones who need to capture this and are called by passing the method to a different function).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With