-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (37 loc) · 1.22 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
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const userRoutes = require("./routes/user");
const bookRoutes = require("./routes/book");
const cartRoutes = require("./routes/cart");
const orderRoutes = require("./routes/order");
const app = express();
const port = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json({ limit: "5mb" })); // Increase the limit for JSON data
app.use(express.urlencoded({ limit: "5mb", extended: true })); // Increase the limit for URL-encoded data
// Connect to MongoDB
mongoose.connect(
"mongodb+srv://root:[email protected]/?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
const db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
db.once("open", () => {
console.log("Connected to MongoDB");
});
// Use the book routes from book.js
app.use("/book", bookRoutes);
// Use the user routes
app.use("/user", userRoutes);
// Use the cart routes
app.use("/cart", cartRoutes);
//use the order routes
app.use("/order", orderRoutes);
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});