-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (59 loc) · 1.89 KB
/
app.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
57
58
59
60
61
62
63
64
65
66
67
68
import "express-async-errors";
import express from "express";
import mongoose from "mongoose";
import session from "express-session";
import dotenv from "dotenv";
import helmet from "helmet";
import mongoSanitize from "express-mongo-sanitize";
import cors from "cors";
import authRoutes from "./routes/authRoute.js";
import userRoutes from "./routes/usersRoute.js";
import profileRoutes from "./routes/profilesRoute.js";
import groupRoutes from "./routes/groupsRoute.js";
import contactRoutes from "./routes/contactsRoute.js";
import productRoutes from "./routes/productsRoutes.js";
import paymentRoutes from "./routes/paymentRoutes.js";
import authMiddleware from "./middlewares/authMiddleware.js";
import errorHandlerMiddleware from "./middlewares/errorHandler.js";
// Access .env files
dotenv.config();
// Create HTTP Server
const app = express();
// Security Packages
app.use(helmet());
app.use(mongoSanitize());
// Body Parser
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(
cors({
origin: "*",
credentials: true,
})
);
// Routers
app.use("/api/v1/auth", authRoutes);
app.use("/api/v1/users", userRoutes);
app.use("/api/v1/profiles", authMiddleware, profileRoutes);
app.use("/api/v1/groups", authMiddleware, groupRoutes);
app.use("/api/v1/contacts", authMiddleware, contactRoutes);
app.use("/api/v1/products", authMiddleware, productRoutes);
app.use("/api/v1/payments", paymentRoutes);
// NOT FOUND Handler
app.use("*", (req, res, next) => {
console.log(req);
res.status(404).json({ message: "Route not found." });
});
// Global Error Handler
app.use(errorHandlerMiddleware);
// Connect to Database and Start listening
try {
await mongoose.connect(process.env.MONGO_URI);
const port = process.env.PORT || 5100;
app.listen(port, () =>
console.log(`Server started listening on PORT ${port}...`)
);
} catch (error) {
console.log(error);
process.exit(1);
}