-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
52 lines (41 loc) · 1.18 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
const express = require('express');
const session = require('express-session');
const dotenv = require('dotenv');
const path = require('path');
// Load environment variables
dotenv.config();
// Create Express app
const app = express();
// Set up session middleware
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
})
);
// Set up body parser middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Set up static files directory
app.use(express.static(path.join(__dirname, 'public')));
// Set up view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Routes
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const adminRouter = require('./routes/admin');
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/admin', adminRouter);
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err);
res.status(500).send('Internal Server Error');
});
// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});