-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (54 loc) · 1.7 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
const logger = require('./logger');
require('dotenv').config();
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const database = require('./database');
const routes = require('./routes/routes');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const flash = require('express-flash');
const cookieParser = require('cookie-parser');
const { default: helmet } = require('helmet');
const PORT = process.env.PORT || 3232;
const app = express();
// Create a MongoDB Session Store
const sessionStore = MongoStore.create({
mongoUrl: process.env.MONGODB_URI,
ttl: 60 * 60,
})
app.use(helmet())
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.use(cookieParser());
// Configure Session Middleware
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: sessionStore
}))
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Database Connection
database.connectToDatabase();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(flash());
// Define Base Path for Routes
app.use('/', routes);
// Page Not Found Route
app.use((req, res) => {
res.status(404).render('error',{
title: "Page Not Found!",
links: [
{ link_name: 'Home', url: '/' },
{ link_name: 'Login', url: '/login' },
{ link_name: 'Logout', url: '/logout' }
],
req,
});
logger.error('Page Not Found!', { status: 404, url: req.originalUrl });
});
app.listen(PORT, () => {
logger.info(`Server started successfully on http://localhost:${PORT}`);
});