-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (31 loc) · 1.01 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
const mongoose = require("mongoose");
const express = require("express");
const dotenv = require("dotenv");
const cors= require("cors");
const bookRoutes = require("./books/bookRoutes");
const userRoutes = require("./users/userRoutes");
dotenv.config();
const app = express();
const corsOption = {
origin: "http://localhost:3000/",
methods: "GET,POST,PUT",
allowedHeaders: ["Content-Type", "Authorization"],
};
app.use(cors(corsOption));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/book", bookRoutes);
app.use("/user", userRoutes);
(async () =>{
try {
await mongoose.connect("mongodb+srv://mongo:[email protected]/bookStoreDb", {});
console.log('Connected to MongoDB');
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
} catch (error) {
console.error('Error connecting to MongoDB', error);
process.exit(1);
}
})();