Task: Add application instance for express and nodejs project

This commit is contained in:
Sambo Chea 2021-09-14 08:31:08 +07:00
parent c7f3aa02b5
commit a6546b7676
3 changed files with 29 additions and 3 deletions

23
src/app/application.ts Normal file
View File

@ -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()

3
src/app/routes.ts Normal file
View File

@ -0,0 +1,3 @@
const index = (req: any, res: any) => {
res.send("Hello World!")
}

View File

@ -1,13 +1,13 @@
import { createServer } from "http" import { createServer } from "http"
import Express from "express"
import { SERVER_PORT } from "./app.config" import { SERVER_PORT } from "./app.config"
import { info } from "@cubetiq/ts-common/dist/log" import { info } from "@cubetiq/ts-common/dist/log"
import application from "./app/application"
// get current host id // get current host id
const hostId = `${require("os").hostname()}#${process.pid}` const hostId = `${require("os").hostname()}#${process.pid}`
const startedAt = new Date() const startedAt = new Date()
const app = Express() const app = application.instance
const httpServer = createServer(app) const httpServer = createServer(app)
info( info(
@ -15,7 +15,7 @@ info(
) )
httpServer.listen(SERVER_PORT) httpServer.listen(SERVER_PORT)
app.get("/", (req, res) => { app.get("/info", (req, res) => {
res.type("json") res.type("json")
res.send({ res.send({
startedAt, startedAt,