-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
41 lines (33 loc) · 896 Bytes
/
main.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
import dotenv from "dotenv";
if (process.env.NODE_ENV != "production") {
dotenv.config();
}
import ExpressLib from "express";
import cors from "cors";
import Logger from "./infra/logger";
import V1_Router from "./routes/v1/index";
const port = process.env.PORT || 3000;
const ApplicationInstance: ExpressLib.Application = ExpressLib();
//setup cors
ApplicationInstance.use(cors({
origin: "*"
}));
//Router
ApplicationInstance.use("/v1", V1_Router);
//error handling
ApplicationInstance.use(
(
error: Error,
request: ExpressLib.Request,
response: ExpressLib.Response,
next: ExpressLib.NextFunction
) => {
const errorData = JSON.parse(error.message);
Logger.warn(error.stack);
response.status(errorData.status_code).json(errorData);
}
);
//init
ApplicationInstance.listen(port, () => {
Logger.log(`Server listening at http://localhost:${port}`);
});