-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
42 lines (38 loc) · 1.85 KB
/
Copy pathapp.js
File metadata and controls
42 lines (38 loc) · 1.85 KB
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
require('dotenv').config(); // Load environment variables from .env file
const express = require('express');
const mongoose = require('mongoose');
const userAuthRoutes = require('./routes/userAuth'); // User authentication routes
const workerAuthRoutes = require('./routes/workerAuth'); // Worker authentication routes
const path = require('path');
const session = require('express-session'); // Required for session management
const app = express();
// Middleware
app.use(express.urlencoded({ extended: true })); // To parse URL-encoded data from forms
app.use(express.json()); // To parse JSON data
app.use('/uploads', express.static(path.join(__dirname, 'public/uploads'))); // Serve uploaded images
app.use(session({
secret: 'your-secret-key', // A secret key to sign the session ID cookie
resave: false, // Don't save session if unmodified
saveUninitialized: true, // Save uninitialized sessions
cookie: { secure: false } // Set to true in production when using HTTPS
}));
app.use(express.static(path.join(__dirname, 'public')));
// Connect to MongoDB
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log('MongoDB Connected'))
.catch(err => console.error('Database Connection Error:', err));
// Set the view engine to EJS
app.set('view engine', 'ejs');
// Assuming your 'first.css' is inside 'public/css/'
app.use('/css', express.static(path.join(__dirname, 'css')));
app.use('/assets', express.static(path.join(__dirname, 'assets')));
// Routes
app.use('/user', userAuthRoutes); // User authentication routes
app.use('/worker', workerAuthRoutes); // Worker authentication routes
// Root Route (Home Page)
app.get('/', (req, res) => {
res.render('first'); // Render 'first.ejs' file for the home page
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));