-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
103 lines (90 loc) · 2.59 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
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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const history = require('connect-history-api-fallback');
const chalk = require('chalk');
const path = require('path');
const morgan = require('morgan');
const contentPath = process.env.NODE_ENV === 'production' ? 'dist' : 'src';
const port = process.env.PORT || 4445;
const session = require('express-session');
const expressValidator = require('express-validator');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const exphbs = require('express-handlebars');
const applyRoutes = require('./routes');
require('dotenv').config();
if (!process.env.AWS_USER_KEY) {
throw Error('Environment files not found!!! Check the .env configuration.');
process.exit(1);
}
// Init Database
require('./db/index');
// Handlebars View For Login Page
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({ defaultLayout: 'main' }));
app.set('view engine', 'handlebars');
// Middleware galore
app.use(morgan('dev')); // logging
app.use(compression()); // gzip
app.use(bodyParser.json()); // process post json
app.use(
// See: https://www.npmjs.com/package/qs#readme
bodyParser.urlencoded({
extended: false // Allows for query strings to be handled like json
})
);
app.use(cookieParser()); // Parses cookies
//Express session
app.use(
session({
secret: 'secret',
saveUninitialized: true,
resave: true
})
);
//Passport init
app.use(passport.initialize());
app.use(passport.session());
//Express Validator
app.use(
expressValidator({
errorFormatter: (param, msg, value) => {
let namespace = param.split('.'),
root = namespace.shift(),
formParam = root;
while (namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param: formParam,
msg: msg,
value: value
};
}
})
);
// API Routes
applyRoutes(app);
// Rendered Routes
app.get('/login', (req, res) => res.render('login'));
app.get('/register', (req, res) => res.render('register'));
app.get('/logout', (req, res) => {
req.logout();
res.redirect('/login');
});
// Static Files (favicon, etc.)
app.use(express.static(path.join(__dirname, 'public')));
// Check for auth, serve react app
app.use((req, res, next) => {
if (req.isAuthenticated()) {
return next();
} else {
res.redirect('/login');
}
}, express.static(path.join(__dirname, contentPath)));
app.listen(port, () => {
console.log(chalk.green(`🌋 [${contentPath}] Listening on port: ${port}`));
});