-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (46 loc) · 1.14 KB
/
server.js
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
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import connect from "./src/db/connect.js";
import cookieParser from "cookie-parser";
import fs from "node:fs";
import errorHandler from "./src/helpers/errorhandler.js";
dotenv.config();
const port = process.env.PORT || 8000;
const app = express();
// middleware
app.use(
cors({
origin: process.env.CLIENT_URL,
credentials: true,
})
);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// error handler middleware
app.use(errorHandler);
//routes
const routeFiles = fs.readdirSync("./src/routes");
routeFiles.forEach((file) => {
// use dynamic import
import(`./src/routes/${file}`)
.then((route) => {
app.use("/api/v1", route.default);
})
.catch((err) => {
console.log("Failed to load route file", err);
});
});
const server = async () => {
try {
await connect();
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
} catch (error) {
console.log("Failed to strt server.....", error.message);
process.exit(1);
}
};
server();