Saturday 6 May 2017

static variable in class is undefined in callback on app.listen

I am just beginning to use typescript instead of javascript for writing my node.js apps. and i am a bit confused.

I have this code which starts an express server import * as express from 'express';

class Server {
    static expressApp: express.Express;
    static PORT: number = 3000;


    public static startServer():void {
        this.expressApp = express();
        console.log(this.PORT);
        this.expressApp.listen(this.PORT, this.serverStartedCallback)
    }

    private static serverStartedCallback():void {
        console.log("Server is listening on port " + this.PORT);
    }
}

Server.startServer();

I can get the value of the PORT variable in startSever(). but in the callback serverStarted() the this.PORT variable is undefined. Can someone elaborate as to why this is?

best regards Morten



via Morten Due Christiansen

No comments:

Post a Comment