-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (47 loc) · 1.91 KB
/
server.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
54
55
56
// server.js
const express = require('express');
const app = express();
const path = require('path');
const mongoose = require('mongoose');
const cron = require('node-cron');
const dotenv = require('dotenv');
const bodyParser = require('body-parser');
// Load environment variables
dotenv.config();
// Initialize Express app
app.use(bodyParser.json());
// Import routes
const userRoutes = require('./routes/userroutes');
const notificationRoutes = require('./routes/notificationroutes');
const industryEventRoutes = require('./routes/industryeventroutes');
const linkRoutes = require('./routes/linkroutes');
const adminRoutes = require('./routes/adminroutes');
// Use routes
app.use('/api/users', userRoutes);
app.use('/api/links', linkRoutes);
app.use('/api/admin', adminRoutes);
app.use('/api/notifications', notificationRoutes);
app.use('/api/industry-events', industryEventRoutes);
// Serve static files from 'uploads' directory
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// Connect to MongoDB and run cron jobs if not in the test environment
if (process.env.NODE_ENV !== 'test') {
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('Error connecting to MongoDB:', err));
// Import and execute schedulers (cron jobs)
require('./utils/cronjobs');
} else {
mongoose.connect(process.env.MONGO_URI_TEST, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB for tests'))
.catch(err => console.error('Error connecting to MongoDB for tests:', err));
}
// Start the server if not in test mode
if (process.env.NODE_ENV !== 'test') {
const PORT = process.env.PORT || 51241;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
}
// Export app for testing
module.exports = app;