-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (46 loc) · 1.46 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
const express = require('express');
const session = require('express-session');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo');
const dotenv = require('dotenv');
// Load environment variables from .env file
dotenv.config();
// Create Express app
const app = express();
const PORT = process.env.PORT || 6969;
// Middleware
app.use(express.json());
app.use(express.static('public'));
mongoose.connect(process.env.MONGODB_URI, {
// useNewUrlParser: true,
// useUnifiedTopology: true
}).then(() => {
console.log('Connected to MongoDB');
}).catch((error) => {
console.error('Error connecting to MongoDB:', error);
process.exit(1); // Exit the process if unable to connect to MongoDB
});
// Set up session middleware
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
store: MongoStore.create({
client: mongoose.connection, // Use Mongoose connection directly
mongoUrl: process.env.MONGODB_URI // Specify MongoDB URL
})
}));
// Import routes
const routes = require('./routes');
// Routes
app.use('/api', routes);
// Start the server
const server = app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// Handle unhandled promise rejections
process.on('unhandledRejection', (error, promise) => {
console.error('Unhandled Rejection:', error);
// Close server and exit process
server.close(() => process.exit(1));
});