-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (168 loc) · 4.16 KB
/
index.js
File metadata and controls
175 lines (168 loc) · 4.16 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import express from "express";
import "dotenv/config";
import "./db/connectDB.js";
import cors from "cors";
import mongoose, { connectDB } from "./db/connectDB.js";
import userSchema from "./models/user-model.js";
import bookSchema from "./models/book-model.js";
import transactionSchema from "./models/transaction-model.js";
import router from "./routes/routes.js";
import routeNotFound from "./middlewares/routeNotFound-middleware.js";
import errorHandler from "./middlewares/errorHandler-middleware.js";
const app = express();
const startServer = async () => {
try {
await connectDB();
const User = mongoose.libraryDB.model('User', userSchema);
const Book = mongoose.libraryDB.model('Book', bookSchema);
const Transaction = mongoose.transactionsDB.model('Transaction', transactionSchema);
app.use((req, res, next) => {
req.User = User;
req.Book = Book;
req.Transaction = Transaction;
next();
});
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/', router);
app.use(routeNotFound);
app.use(errorHandler);
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`Server is listening on Port: ${port}`);
});
} catch (error) {
console.error("Error connecting to databases:", error.stack);
}
}
startServer();
// const users = [
// {
// name: "John Doe",
// email: "john.doe@example.com",
// phone: "1234567890"
// },
// {
// name: "Jane Smith",
// email: "jane.smith@example.com",
// phone: "9876543210"
// },
// {
// name: "Alice Johnson",
// email: "alice.johnson@example.com",
// phone: "1112223333"
// },
// {
// name: "Bob Brown",
// email: "bob.brown@example.com",
// phone: "4445556666"
// },
// {
// name: "Charlie White",
// email: "charlie.white@example.com",
// phone: "7778889999"
// }
// ]
// const books = [
// {
// name: "The Great Gatsby",
// category: "Fiction",
// rent: 5
// },
// {
// name: "To Kill a Mockingbird",
// category: "Fiction",
// rent: 4
// },
// {
// name: "1984",
// category: "Dystopian",
// rent: 6
// },
// {
// name: "Pride and Prejudice",
// category: "Romance",
// rent: 4
// },
// {
// name: "Moby Dick",
// category: "Adventure",
// rent: 7
// },
// {
// name: "The Catcher in the Rye",
// category: "Fiction",
// rent: 5
// },
// {
// name: "The Hobbit",
// category: "Fantasy",
// rent: 6
// },
// {
// name: "War and Peace",
// category: "Historical",
// rent: 8
// },
// {
// name: "Brave New World",
// category: "Dystopian",
// rent: 5
// },
// {
// name: "The Odyssey",
// category: "Epic",
// rent: 7
// },
// {
// name: "Crime and Punishment",
// category: "Mystery",
// rent: 6
// },
// {
// name: "The Lord of the Rings",
// category: "Fantasy",
// rent: 8
// },
// {
// name: "The Divine Comedy",
// category: "Epic",
// rent: 6
// },
// {
// name: "Anna Karenina",
// category: "Romance",
// rent: 5
// },
// {
// name: "Don Quixote",
// category: "Adventure",
// rent: 7
// },
// {
// name: "Hamlet",
// category: "Tragedy",
// rent: 4
// },
// {
// name: "The Iliad",
// category: "Epic",
// rent: 7
// },
// {
// name: "Frankenstein",
// category: "Horror",
// rent: 5
// },
// {
// name: "Dracula",
// category: "Horror",
// rent: 6
// },
// {
// name: "The Picture of Dorian Gray",
// category: "Gothic",
// rent: 4
// }
// ]