Created
March 16, 2022 20:21
-
-
Save thakursaurabh1998/f9034c61d3a3efbd056d9a84bac897f1 to your computer and use it in GitHub Desktop.
Singleton Pattern in TS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express, { Application } from "express"; | |
export default class Server { | |
private app: Application | |
private static instance: Server; | |
private constructor() { | |
this.app = express(); | |
this.app.listen(8000, () => { | |
console.log("Server started"); | |
}); | |
} | |
static start() { | |
if (Server.instance) { | |
console.log('Server already running'); | |
return this.instance; | |
} | |
this.instance = new Server(); | |
console.log("Running on port 8000"); | |
return this.instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment