-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (60 loc) · 2.16 KB
/
app.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
70
71
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const cookieParser = require('cookie-parser');
const expressSession = require('express-session');
const flash = require('connect-flash');
const config = require('./utils/config'); // Centralized configuration
const app = express();
// Routes
const authRoutes = require('./routes/authRoutes');
const resumeRoutes = require('./routes/resumeRoutes');
const phoneRoutes = require('./routes/phoneRoutes');
const profileRoutes = require('./routes/profileRoutes');
const dashboardRoutes = require('./routes/dashboardRoutes');
// Middleware setup
app.use(cookieParser());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Set up express-session for flash messages
app.use(expressSession({
secret: config.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
secure: config.NODE_ENV === 'production',
sameSite: 'strict',
maxAge: 3600000,
}
}));
// Set up flash middleware
app.use(flash());
// Set EJS as the templating engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Static files (CSS, JS, etc.)
app.use(express.static(path.join(__dirname, 'public')));
// Middleware to set flash messages in local variables
app.use((req, res, next) => {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
next();
});
// MongoDB connection
mongoose.connect(config.MONGODB_URI)
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Failed to connect to MongoDB', err));
// Route for the Home Page
app.get('/', (req, res) => {
return res.render('home'); // Render the home.ejs file
});
// Modularized routes
app.use('/auth', authRoutes); // Authentication routes
app.use('/resume', resumeRoutes); // Resume routes
app.use('/profile', profileRoutes); // Profile routes
app.use('/api/phones', phoneRoutes); // Phone routes
app.use('/dashboard', dashboardRoutes); // Dashboard routes
// Start the server
app.listen(config.PORT, () => {
console.log(`Server running on http://localhost:${config.PORT}`);
});