Skip to content

Instantly share code, notes, and snippets.

@thakursaurabh1998
Created March 16, 2022 20:21
Show Gist options
  • Save thakursaurabh1998/f9034c61d3a3efbd056d9a84bac897f1 to your computer and use it in GitHub Desktop.
Save thakursaurabh1998/f9034c61d3a3efbd056d9a84bac897f1 to your computer and use it in GitHub Desktop.
Singleton Pattern in TS
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