-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
75 lines (59 loc) · 2.03 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
const serverless = require('serverless-http');
// Initialize express
const express = require('express')
const methodOverride = require('method-override')
const app = express()
const dotenv = require("dotenv").config();
// INITIALIZE BODY-PARSER AND ADD IT TO APP
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
var cookieParser = require('cookie-parser');
const jwt = require('jsonwebtoken');
const handlebars = require('handlebars');
const { allowInsecurePrototypeAccess } = require('@handlebars/allow-prototype-access');
const exphbs = require('express-handlebars');
const hbs = exphbs.create({
defaultLayout: 'main',
handlebars: allowInsecurePrototypeAccess(handlebars),
});
app.engine('handlebars', hbs.engine);
// Use handlebars to render
app.set('view engine', 'handlebars');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
// override with POST having ?_method=DELETE or ?_method=PUT
app.use(methodOverride('_method'))
app.use(express.static('public'));
// Auth Setup
app.use(express.static(__dirname));
app.use(bodyParser.json());
const checkUser = require("./utils/checkUser")
app.use(checkUser)
app.use(cookieParser());
var checkAuth = (req, res, next) => {
console.log("Checking authentication");
if (typeof req.cookies.nToken === "undefined" || req.cookies.nToken === null) {
req.user = null;
} else {
var token = req.cookies.nToken;
var decodedToken = jwt.decode(token, { complete: true }) || {};
req.user = decodedToken.payload;
}
next();
};
app.use(checkAuth);
// controllers
require('./db/whiskersdb')
require('./controllers/dogs')(app);
require('./controllers/favorites')(app);
require('./controllers/comments')(app);
require('./controllers/auth')(app);
require('./controllers/replies')(app);
// Choose a port to listen on
const port = process.env.PORT || 8080;
// Tell the app what port to listen on
app.listen(port, () => {
console.log('App listening on port 8080!')
})
module.exports.handler = serverless(app);