-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
69 lines (58 loc) · 2.06 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
57
58
59
60
61
62
63
64
65
66
67
68
69
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const session = require('express-session');
const fileUpload = require('express-fileupload');
const app = express();
const route = require('./routes/quiklearn'); // Route for main application
const route1 = require('./routes/teacherroutes'); // Route for teacher-related endpoints
const route2 = require('./routes/studentroutes'); // Route for student-related endpoints
const route3 = require('./routes/adminRoutes'); // Route for admin-related endpoints
// Connect to MongoDB Atlas
const connectMongoDB = async (uri) => {
try {
await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
});
console.log('MongoDB connected');
} catch (err) {
console.error('MongoDB connection error:', err);
}
};
// Express middleware setup
app.use(express.urlencoded({ extended: true }));
app.use(fileUpload());
// Serve static files
app.use('/uploads/images', express.static(path.join(__dirname, 'uploads/images')));
app.use(express.static(path.join(__dirname, "public")));
// Set the view engine (if you're using one)
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Session middleware
app.use(session({
secret: 'simpleSecretKey', // Hardcoded secret key
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // Set to true if using HTTPS
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}
}));
// Route handling
app.use('/', route);
app.use('/', route3);
app.use('/',route1);
app.use('/', route2);
// Error handling for undefined routes
app.use((req, res) => {
res.status(404).send("Sorry, that route doesn't exist.");
});
// Connect to MongoDB and start the server
const PORT = process.env.PORT || 3000;
const MONGODB_URI = "mongodb+srv://Project:[email protected]/quiklearn";
connectMongoDB(MONGODB_URI).then(() => {
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
});