-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathserver.ts
More file actions
41 lines (35 loc) · 1.25 KB
/
server.ts
File metadata and controls
41 lines (35 loc) · 1.25 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
import express from "express";
import cors from "cors";
import mongoose from "mongoose";
import homeRouter from "./pages/home";
import availableRouter from "./pages/books_status";
import bookRouter from "./pages/books";
import authorRouter from "./pages/authors";
import createBookRouter from "./pages/create_book";
import bookDetailsRouter from "./pages/book_details";
// Create express app
const app = express();
// Middleware
app.use(cors());
app.use('/home', homeRouter);
app.use('/available', availableRouter);
app.use('/books', bookRouter);
app.use('/authors', authorRouter);
app.use('/book_dtls', bookDetailsRouter);
app.use('/newbook', createBookRouter);
// Start server only if not in test environment
if (process.env.NODE_ENV !== "test") {
const port = 8000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
// Connect to MongoDB only in non-test environments
const mongoDB = 'mongodb://127.0.0.1:27017/my_library_db';
mongoose.connect(mongoDB);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
db.on('connected', () => {
console.log('Connected to database');
});
}
export default app; // Export only the Express app