From a6546b7676a4531c47377113e6c44d6b75620496 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Tue, 14 Sep 2021 08:31:08 +0700 Subject: [PATCH] Task: Add application instance for express and nodejs project --- src/app/application.ts | 23 +++++++++++++++++++++++ src/app/routes.ts | 3 +++ src/server.ts | 6 +++--- 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 src/app/application.ts create mode 100644 src/app/routes.ts diff --git a/src/app/application.ts b/src/app/application.ts new file mode 100644 index 0000000..a597d54 --- /dev/null +++ b/src/app/application.ts @@ -0,0 +1,23 @@ +import express, { Application as ExpressApp } from "express" + +class Application { + private readonly _instance: ExpressApp + + get instance(): ExpressApp { + return this._instance + } + + constructor() { + this._instance = express() + this._instance.use(express.json()) + this.registerRoutes() + } + + private registerRoutes(): void { + this._instance.get("/", (req, res) => { + res.send("Hello World!") + }) + } +} + +export default new Application() diff --git a/src/app/routes.ts b/src/app/routes.ts new file mode 100644 index 0000000..9a4b9e1 --- /dev/null +++ b/src/app/routes.ts @@ -0,0 +1,3 @@ +const index = (req: any, res: any) => { + res.send("Hello World!") +} diff --git a/src/server.ts b/src/server.ts index 2850e1b..546c6e0 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,13 +1,13 @@ import { createServer } from "http" -import Express from "express" import { SERVER_PORT } from "./app.config" import { info } from "@cubetiq/ts-common/dist/log" +import application from "./app/application" // get current host id const hostId = `${require("os").hostname()}#${process.pid}` const startedAt = new Date() -const app = Express() +const app = application.instance const httpServer = createServer(app) info( @@ -15,7 +15,7 @@ info( ) httpServer.listen(SERVER_PORT) -app.get("/", (req, res) => { +app.get("/info", (req, res) => { res.type("json") res.send({ startedAt,