-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (41 loc) · 1.28 KB
/
index.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
49
50
51
52
53
// Import required modules
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const swaggerUi = require('swagger-ui-express');
const swaggerSpec = require('./swagger');
require('dotenv').config();
const productRoutes = require("./routes/productRoute");
const diseaseRoutes = require("./routes/diseaseRoute");
const communityRoutes = require("./routes/communityRoute");
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(cors());
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on('error', (err) => {
console.error('MongoDB connection error:', err);
});
db.once('open', () => {
console.log('Connected to MongoDB');
});
app.get('/', async (req, res) => {
try {
res.status(200).send('Hello World!');
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.use('/api', productRoutes);
app.use('/api', diseaseRoutes);
app.use('/api', communityRoutes);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});