-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
55 lines (45 loc) · 1.89 KB
/
server.ts
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
import express, { Express, Request, Response, NextFunction } from 'express';
import bodyParser from 'body-parser';
import mongoose, { ConnectionStates } from 'mongoose';
import BookModel from './models/bookModel'; // Update based on your actual file structure
import bookRoutes from './routes/bookRoutes'; // Update based on your actual file structure
const app: Express = express();
const port: string | number = process.env.PORT || 3000;
// Use native promises with Mongoose
mongoose.Promise = global.Promise;
const username = encodeURIComponent('oludotunlonge');
const password = encodeURIComponent('oVMkevF6DB3palTX');
const clusterUrl = 'cluster0.1rhcq3z.mongodb.net';
const dbName = 'books'; // replace with your database name
// Database connection
const dbUrl: string = process.env.DB_URL || `mongodb+srv://${username}:${password}@${clusterUrl}/${dbName}?retryWrites=true&w=majority`;
// Middleware to check database connection
const checkDatabaseConnection = (req: Request, res: Response, next: NextFunction) => {
// Mongoose connection states: 0 = disconnected, 1 = connected, 2 = connecting, 3 = disconnecting
const isConnected = mongoose.connection.readyState === 1;
if (!isConnected) {
mongoose.connect(dbUrl, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('Successfully connected to the database');
next();
})
.catch(err => {
console.error('Database connection error', err);
});
// return res.status(503).send('Database is not connected');
}else {
next();
}
};
// Body parser middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Use the checkDatabaseConnection middleware in your routes
app.use(checkDatabaseConnection);
// Routes
bookRoutes(app);
// Start server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
export default app;