-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
67 lines (61 loc) · 1.76 KB
/
server.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
import express, { Application, Request, Response, NextFunction } from "express";
import bodyParser from "body-parser";
import cors from "cors";
import logger from "morgan";
import router from "./api/routes";
import { Error } from "./api/responses";
import { GetEnv } from "./config";
import { Migration } from "./migration";
const app: Application = express();
const PORT = GetEnv("PORT") || 9000;
app.use(cors());
app.use(function (req: Request, res: Response, next: NextFunction) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, PATCH, DELETE"
);
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader(
"Access-Control-Allow-Headers",
"X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization"
);
if (req.method === "OPTIONS") {
res
.status(200)
.header("Content-Length", "0")
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "GET,HEAD,PUT,PATCH,POST,DELETE")
.send();
}
next();
});
app.use(express.json());
app.use(bodyParser.json({ limit: "150mb" }));
app.use(logger("combined"));
app.use(express.urlencoded({ extended: true }));
app.use("/", router);
app.use((next: NextFunction) => {
const error: Error = {};
error.message = "Not Found";
error.status = {
success: false,
code: 404,
};
error.error = {
message: "Route Not Found",
};
next(error);
});
app.use((error: any, req: Request, res: Response) => {
console.error(error);
res.status(error?.status?.code || 500).json(error);
});
app.listen(PORT, async () => {
try {
await Migration();
console.log(`Server is running at ${PORT}`);
} catch (e) {
console.error(e);
}
});