-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (51 loc) · 1.61 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
const path = require("path");
const express = require("express");
const mongoose = require("mongoose");
const morgan = require("morgan");
const cors = require("cors");
// Routes
const userRouter = require("./routers/userRouter");
const productRouter = require("./routers/productRouter");
const uploadRouter = require("./routers/uploadRouter");
const orderRouter = require("./routers/orderRouter");
// const cartRouter = require("./routes/cartRouter")
// Load Config
require("dotenv").config();
// Connect to MongoDB
const connectDB = async () => {
try {
const connect = await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log(`MongoDB Connected ${connect.connection.host}`);
} catch (err) {
console.error("MongoDB Connection FAIL");
process.exit(1);
}
};
connectDB();
const app = express();
app.use(morgan("dev"));
app.use(cors());
// Body-Parser
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Static Folder
app.use(express.static(path.join(__dirname, "uploads")));
// SET Routers
app.use("/api/uploads", uploadRouter);
app.use("/api/users", userRouter);
app.use("/api/products", productRouter);
app.use("/api/orders", orderRouter);
// app.use("/api/cart", cartRouter);
app.get("/api/config/paypal", (req, res) => {
res.send(process.env.PAYPAL_CLIENT_ID || "sb");
});
// Error Handling
app.use((err, req, res, next) => {
res.status(500).send({ message: err.message });
});
app.listen(process.env.PORT || 8000, () => {
console.log(`Server running on port: ${process.env.PORT} Time: ${new Date()}`);
});