forked from Harduex/media-cdn-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
79 lines (63 loc) · 1.62 KB
/
app.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import express, { Application } from "express";
// Utilities
import path from "path";
import cors from "cors";
import morgan from "morgan";
import fs from "fs";
// Configurations
import dotenv from "dotenv";
dotenv.config();
import config from "config";
// https
import https from "https";
// Routes
import useRoutes from "./routes";
// Custom logger
import logger from "./App/Helpers/utilities/logger";
// Custom Middlewares
import { error404, errorHandler } from "./App/Helpers/middlewares/errorHandler";
// Port
const port: number = config.get<number>("server.port");
// App
const app: Application = express();
// Cors
const corsOrigin: string = config.get<string>("server.corsOrigin");
app.use(
cors({
origin: corsOrigin,
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true,
})
);
// Static path
const staticFolder = config.get<string>("server.staticFolder");
// Utilities
// Depreciate: useless because function was rewritten
// app.use(express.static(path.join(__dirname, staticFolder)));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(morgan("dev"));
// Use routes
useRoutes(app);
// Error handling
app.use(error404);
app.use(errorHandler);
if (config.get<boolean>("server.https")) {
// Https Server
https
.createServer(
{
key: fs.readFileSync("./key.pem"),
cert: fs.readFileSync("./cert.pem"),
},
app
)
.listen(port, function () {
logger.info(`HTTPS Server listening on port ${port}`);
});
} else {
// Http server
app.listen(port, async () => {
logger.info(`HTTP Server listening on port ${port}`);
});
}