-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (46 loc) · 1.55 KB
/
index.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
import "dotenv/config";
import express from "express";
import morgan from "morgan";
import cors from "cors";
import { apiPrefixes, port } from "./config/config.js";
import { connectDB } from "./database/db.js";
import { adminRouter } from "./routes/adminRoutes.js";
import { errorHandler } from "./middlewares/errorHandler.js";
import { riderRouter } from "./routes/riderRoutes.js";
import { userRouter } from "./routes/userRoutes.js";
import { generalRouter } from "./routes/generalRoutes.js";
/* Initial express application */
const app = express();
/* Middlewares */
app
.use(
cors({
origin: "https://food-app-front-end-sand.vercel.app",
// origin: "http://localhost:3000",
credentials: true,
})
)
.use(morgan("dev")) // Logging requests and response
.use(express.json()) // Parse incoming requests with JSON payload
.use(express.urlencoded({ extended: true })) // Parses incoming requests with urlencoded payloads
.use("/images/public", express.static("public"))
/* Router for admin routes */
.use(apiPrefixes.generalApi, generalRouter)
.use(apiPrefixes.userApi, userRouter)
.use(apiPrefixes.riderApi, riderRouter)
.use(apiPrefixes.adminApi, adminRouter)
.use("*", (req, res) =>
res.status(404).json({
status: "error",
message: "route not found",
})
)
/* Error handler middlewares */
.use(errorHandler);
/* Connect to database and Listen for requests using an IIFE*/
(async function () {
await connectDB();
app.listen(port, () => {
console.log(`server is listening on ${port}`);
});
})();