2023-09-01 12:23:50 +00:00
|
|
|
import * as express from "express"
|
|
|
|
import * as bodyParser from "body-parser"
|
|
|
|
import { Request, Response } from "express"
|
|
|
|
import { AppDataSource } from "./data-source"
|
|
|
|
import { Routes } from "./routes"
|
|
|
|
|
|
|
|
AppDataSource.initialize().then(async () => {
|
|
|
|
|
|
|
|
// create express app
|
|
|
|
const app = express()
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
|
|
|
|
// register express routes from defined application routes
|
|
|
|
Routes.forEach(route => {
|
|
|
|
(app as any)[route.method](route.route, (req: Request, res: Response, next: Function) => {
|
|
|
|
const result = (new (route.controller as any))[route.action](req, res, next)
|
|
|
|
if (result instanceof Promise) {
|
|
|
|
result.then(result => result !== null && result !== undefined ? res.send(result) : undefined)
|
|
|
|
} else if (result !== null && result !== undefined) {
|
|
|
|
res.json(result)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// setup express app here
|
|
|
|
// ...
|
|
|
|
|
|
|
|
// start express server
|
2023-09-01 14:36:01 +00:00
|
|
|
app.listen(3501)
|
2023-09-01 12:23:50 +00:00
|
|
|
|
|
|
|
// insert new users for test
|
2023-09-01 14:36:01 +00:00
|
|
|
// await AppDataSource.manager.save(
|
|
|
|
// AppDataSource.manager.create(User, {
|
|
|
|
// firstName: "Timber",
|
|
|
|
// lastName: "Saw",
|
|
|
|
// age: 27
|
|
|
|
// })
|
|
|
|
// )
|
|
|
|
|
|
|
|
// await AppDataSource.manager.save(
|
|
|
|
// AppDataSource.manager.create(User, {
|
|
|
|
// firstName: "Phantom",
|
|
|
|
// lastName: "Assassin",
|
|
|
|
// age: 24
|
|
|
|
// })
|
|
|
|
// )
|
|
|
|
|
|
|
|
console.log("Express server has started on port 3501. Open http://localhost:3501/ to see results")
|
2023-09-01 12:23:50 +00:00
|
|
|
|
|
|
|
}).catch(error => console.log(error))
|