forked from ashiishme/ecometer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
42 lines (37 loc) · 1.07 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import dotenv from "dotenv";
dotenv.config();
import express from "express";
import cors from "cors";
import { getEnv } from "./getEnv";
import { routes } from "./routes";
import dataSource from "./database/config";
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
routes.forEach(({ path, method, controller }) => {
const route = `/api/v1${path}`;
app[method](route, async (req, res) => {
try {
await new controller().handle(req, res);
} catch (err) {
console.error(err);
res.status(500).send("Internal server error");
}
});
console.info(`==> API Route ${route} registered with ${method} method`);
});
const PORT = Number(getEnv("PORT")) || 8080;
// startup
(async () => {
try {
await dataSource.initialize();
console.log("Database connection successful");
app.listen(PORT, () => {
console.log(`Server started at: http://localhost:${PORT}`);
});
} catch (error) {
console.error("Unable to connect with a database: ", error);
process.exit(1);
}
})();