-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (96 loc) · 3.71 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Express Imports and app Constant
const express = require('express')
// const multer = require('multer') // Multer - file upload processing
const hbs = require('hbs') // Handlebars - templating
const session = require('express-session') // Express-Session - Server-side client session handling
const MongoStore = require('connect-mongo') // Express-Session - Session Storage in MongoDB
const cookieParser = require('cookie-parser') // (Session dependency) - Cookie parsing
const app = express()
// Error Handling Imports
const createError = require('http-errors')
// Utility Imports
const path = require('path')
const logger = require('morgan')
// --- Multer Image Storage Setup ---
// const iconStorage = multer.diskStorage({
// destination: './public/images/uploaded/user_icon',
// filename: function (req, file, cb) {
// cb(null, Date.now() + path.extname(file.originalname))
// }
// })
// const upload = multer({ iconStorage })
// --- Router Imports ---
const indexRouter = require('./routes/index')
const profileRouter = require('./routes/profile')
const projectRouter = require('./routes/project')
const registerRouter = require('./routes/register')
const loginRouter = require('./routes/login')
const termsRouter = require('./routes/terms')
const privacyRouter = require('./routes/privacy')
const logoutRouter = require('./routes/logout')
const newRouter = require('./routes/new')
const trendingRouter = require('./routes/trending')
const userSettingsRouter = require('./routes/user_settings')
const guildsRouter = require('./routes/guilds')
const guildRouter = require('./routes/guild')
const messagesRouter = require('./routes/messages')
// --- Handlebars View Engine Setup ---
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'hbs')
// --- Handlebars Helpers ---
hbs.registerHelper("formatDate", function(date) {
let unformattedDate = new Date(date)
return String(unformattedDate.toISOString().slice(0, 10)) // Return only date portion of string
});
hbs.registerHelper("formatTimestamp", function(timestamp) {
let unformattedTime = new Date(timestamp)
return String(unformattedTime.toString())
});
hbs.registerHelper('equals', function(arg1, arg2, options) {
return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});
// --- Middleware Setup ---
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(cookieParser())
app.use(session({
secret: 'GuildCoderSecretKey',
cookie: {
maxAge: 3600000, // 1 hour (milliseconds)
sameSite: 'strict', // Set SameSite to Strict same-site enforcement
},
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: "mongodb+srv://<username>:<password>@seneca-web.vw4knv6.mongodb.net/web322_week8" })
}))
app.use(express.static(path.join(__dirname, 'public')))
// --- Routers ---
app.use('/', indexRouter)
app.use('/project', projectRouter)
app.use('/profile', profileRouter)
app.use('/register', registerRouter)
app.use('/login', loginRouter)
app.use('/terms', termsRouter)
app.use('/privacy-policy', privacyRouter)
app.use('/logout', logoutRouter)
app.use('/new', newRouter)
app.use('/trending', trendingRouter)
app.use('/settings', userSettingsRouter)
app.use('/guilds', guildsRouter)
app.use('/guild', guildRouter)
app.use('/messages', messagesRouter)
// -- Forward 404s to error handler --
app.use(function (req, res, next) {
next(createError(404))
})
// --- Error Handler ---
app.use(function (err, req, res) {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
// render the error page
res.status(err.status || 500)
res.render('error', { title: 'Error' })
})
module.exports = app