-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
132 lines (93 loc) · 3.37 KB
/
Copy pathserver.js
File metadata and controls
132 lines (93 loc) · 3.37 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
*/
/*
* Pending tasks:
//TODO: Find a way to attach the user object to request. (It's asynchronous)
//TODO: Route to the correct pages
//TODO: Create protected routes
//TODO: Create task models
TODO: Create test todo list page
TODO: Add post rotues for writing/editing tasks
*/
/* *****************
* Initial Setup
*******************/
const express = require('express')
const path = require('path')
const session = require('cookie-session')
const dotenv = require('dotenv').config()
// * Helmet offers protection from web vulnerabilites
// * by setting HTTP headers appropiately.
// const helmet = require('helmet')
const livereload = require('livereload')
const connectLivereload = require('connect-livereload')
// To help us use hbs
const hbs = require('hbs')
const app = express()
// Setup passport
const passport = require('passport')
// Serving our static files within the public folder
const parentDir = path.resolve(path.dirname(__filename), '.')
const publicPath = path.join(parentDir, 'public')
// * to Dynacmically update content in our page
// const liveReloadServer = livereload.createServer().watch(publicPath)
/* ****************
* Redis Configuration
*******************/
// Setup redis store
// const connectRedis = require('connect-redis')
// const { createClient } = require('redis')
// Create an instance of Connect Redis
// const RedisStore = connectRedis(session)
// const redisClient = createClient()
// redisClient.connect().catch(console.error);
/* *****************
* Middleware
*******************/
// Set up requests to be in json format
app.use(express.json())
app.use(express.urlencoded({extended: true}))
app.use(express.static(publicPath))
// Views setup
app.set('view engine', 'hbs')
// Setup the session middleware
app.set('trust proxy', 1)
app.use(session({
cookie: {
// store: new RedisStore({client: redisClient}),
sameSite: true,
secure: true, //False for now while in development
httpOnly: true, // if true prevent client side JS from reading the cookie
//set maxAge later
maxAge: 60000 * 60 * 730 // Approx one month
},
resave: false, // force session to be saved back to session store, even if it wasn't modified during request.
secret: process.env.SESSION_SECRET, // TODO: more secure secret
saveUninitialized: true, // save an unitialized session to the store.
}))
// Initialize passport
app.use(passport.initialize());
//* This middleware alters the request object and is able to
//* attach a ‘user’ value that can be retrieved from the session id.
app.use(passport.session());
// Tell the app to use the router
const router = require('./src/routes');
app.use(router)
// Configure the passport strategies
const { passportConfig } = require('./src/utils/passport')
passportConfig()
// * Setting up helmet and other security options
// app.use(helmet())
app.disable('x-powered-by')
/* ******************
* HBS Configuration
*********************/
const partialsPath = path.join(parentDir, 'views/partials')
hbs.registerPartials(partialsPath)
// Expose our app to hbs so we can access locals
hbs.localsAsTemplateData(app)
// TODO: Add the partials to app.locals?? Access the partial in a route, send it through res.locals? Render?
/***************************************** */
app.listen(process.env.PORT, (err) => {
console.log(`Listening on port ${process.env.PORT}`)
})