-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
73 lines (66 loc) · 1.65 KB
/
server.ts
File metadata and controls
73 lines (66 loc) · 1.65 KB
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
import express, { Express, NextFunction, Request, Response } from "express";
import dotenv from "dotenv";
import mongoose from "mongoose";
import chalk from "chalk";
import cors from "cors";
import licenceRouter from "./src/routes/licence.route";
dotenv.config();
// Express App
const app: Express = express();
const port = process.env.PORT;
const MONGODB_URI = process.env.MONGODB_URI as string;
// Middleware
app.use(
cors({
origin: process.env.FRONTEND_URL as string,
})
);
app.use(
express.json({
limit: "50mb",
})
);
app.use(express.urlencoded({ limit: "50mb" }));
app.use((req: Request, res: Response, next: NextFunction) => {
const reqColor = (method: string, path: string) => {
if (path.includes("/search/")) {
return "bgMagenta";
}
switch (method) {
case "GET":
return "bgCyan";
case "POST":
return "bgGreen";
case "DELETE":
return "bgRed";
case "PATCH":
return "bgYellow";
default:
return "bgWhite";
}
};
console.log(chalk[reqColor(req.method, req.path)](req.method, req.path));
next();
});
// Routes
app.get("/", (req: Request, res: Response) => {
res.json({ msg: "🌍 Welcome to the app !" });
});
app.use("/api/autodeskLicence", licenceRouter);
// Connect to database
mongoose
.connect(MONGODB_URI)
.then(() => {
// Listen requests
app.listen(port, () => {
console.log(chalk.yellow("📂[mongodb]: Connected to database"));
console.log(
chalk.yellow(
`⚡️[server]: Server is running at http://localhost:${port}`
)
);
});
})
.catch((error) => {
console.log(error.message);
});