Created
July 11, 2020 15:57
-
-
Save marco-souza/4d5877102e6d0889ad0c186323869fbc to your computer and use it in GitHub Desktop.
Typescript/Node Dependency Injector (DI) - Koa server sample
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 * as Koa from "koa"; | |
let koaInstance: Koa; | |
class AppInjector { | |
static injectApp() { | |
if (!koaInstance) { | |
koaInstance = new Koa(); | |
koaInstance.use(async (ctx) => { | |
ctx.body = "Hello World"; | |
}); | |
} | |
return koaInstance; | |
} | |
static injectPort() { | |
return 3000; | |
} | |
} | |
class Server { | |
private _app: Koa; | |
private _port: number; | |
constructor() { | |
this._app = AppInjector.injectApp(); | |
this._port = AppInjector.injectPort(); | |
} | |
start() { | |
this._app.listen(this._port); | |
} | |
} | |
const main = () => { | |
const server = new Server(); | |
server.start(); | |
}; | |
main(); |
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 * as Koa from "koa"; | |
class Server { | |
private _app: Koa; | |
private _port: number; | |
constructor(port: number) { | |
this._app = new Koa(); | |
this._port = port; | |
this._app.use(async (ctx) => { | |
ctx.body = "Hello World"; | |
}); | |
} | |
start() { | |
this._app.listen(this._port); | |
} | |
} | |
const main = () => { | |
const server = new Server(3000); | |
server.start(); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment