Task: Add application instance for express and nodejs project
This commit is contained in:
parent
c7f3aa02b5
commit
a6546b7676
23
src/app/application.ts
Normal file
23
src/app/application.ts
Normal 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
3
src/app/routes.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
const index = (req: any, res: any) => {
|
||||||
|
res.send("Hello World!")
|
||||||
|
}
|
@ -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,
|
||||||
|
Loading…
Reference in New Issue
Block a user