-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (51 loc) · 1.66 KB
/
server.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
require('dotenv').config();
const express = require('express')
const bodyParser = require('body-parser')
const exphbs = require('express-handlebars');
const expressValidator = require('express-validator');
var cookieParser = require('cookie-parser');
const jwt = require('jsonwebtoken');
// Set App Variable
const app = express()
app.use(cookieParser()); // Add this after you initialize express.
// Use Body Parser
app.use(expressValidator());
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
var checkAuth = (req, res, next) => {
console.log("Checking authentication");
if (typeof req.cookies.nToken === "undefined" || req.cookies.nToken === null) {
req.user = null;
console.log("No User", req.user)
} else {
console.log("USER CONNECTED", req.user)
var token = req.cookies.nToken;
var decodedToken = jwt.decode(token, { complete: true }) || {};
req.user = decodedToken.payload;
}
next();
};
app.use(checkAuth);
app.use(express.static('public'));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use((req, res, next) => {
const now = new Date().toString()
console.log(`Requested ${req.url} at ${now}`)
next()
})
// Database Setup
// require('./config/db-setup.js')
// Routes
require('./data/reddit-db');
const router = require('./controllers/index.js') // I only need to require one since the index will contain all other routes
app.use(router)
app.get('/', (req, res) => {
var currentUser = req.user;
res.render('homePage', {currentUser})
})
// Start Server
app.listen(3000, () => {
console.log('connection successful')
})
module.exports = app