-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (37 loc) · 1.17 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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
require('dotenv').config();
const userRoutes = require('./routes/userRoutes');
const adRoutes = require('./routes/adRoutes');
const displayRoutes = require('./routes/displayRoutes');
const { authenticate } = require('./middleware/auth');
const app = express();
// Middleware
app.use(cors());
app.use(bodyParser.json());
app.use(express.json({ limit: '100mb' }));
app.use(express.urlencoded({ limit: '100mb', extended: true }));
// API Routes
app.use("/user", userRoutes);
app.use("/display", displayRoutes);
app.use("/api", authenticate, adRoutes);
// MongoDB Connection
const connectDB = async () => {
if (mongoose.connection.readyState === 0) {
try {
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('MongoDB connected successfully');
} catch (err) {
console.error('MongoDB connection error:', err);
throw new Error('Failed to connect to MongoDB');
}
}
};
connectDB();
// Export for Serverless
module.exports = app;