Skip to content

Instantly share code, notes, and snippets.

@marco-souza
Created July 11, 2020 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marco-souza/4d5877102e6d0889ad0c186323869fbc to your computer and use it in GitHub Desktop.
Save marco-souza/4d5877102e6d0889ad0c186323869fbc to your computer and use it in GitHub Desktop.
Typescript/Node Dependency Injector (DI) - Koa server sample
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();
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