This repository has been archived by the owner on Nov 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
102 lines (74 loc) · 2.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
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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').load();
}
require('babel-register')({
presets: ['react'],
});
// Loads Express and creates app object
const express = require('express');
const app = express();
// Loads path
const path = require('path');
// Loads Body parser
const bodyParser = require('body-parser');
// Loads react libraries
const React = require('react');
const ReactDOMServer = require('react-dom/server');
// Loads less middleware
const lessMiddleware = require('less-middleware');
// Loads request library
// const request = require('request')
// Loads passport for authentication
const passport = require('passport');
const flash = require('connect-flash');
// Request logger
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const session = require('express-session');
// File and folder finding module
const find = require('find');
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for authentication)
app.use(bodyParser()); // get information from html forms
app.use((req, res, next) => {
if (req.url !== '/favicon.ico') {
return next();
}
res.status(200);
res.header('Content-Type', 'image/x-icon');
res.header('Cache-Control', 'max-age=4294880896');
res.end();
return null;
});
// Here is where we load the api routes. We put them here so passport deserializer
// is not called everytime we make an api call to them
require('./config/api.js')(app);
// Sets less middleware
app.use(lessMiddleware('/less', {
dest: '/css',
pathRoot: path.join(__dirname, 'public'),
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Sets public directory
app.use(express.static(`${__dirname}/public`));
require('./config/passport')(passport); // pass passport for configuration
// required for passport
const sessionSecret = process.env.SESSION_SECRET !== undefined ? process.env.SESSION_SECRET : 'undefined';
app.use(session({ secret: sessionSecret }));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// Sets get routes. Files are converted to react elements
find.fileSync(/\.js$/, `${__dirname}/controllers`).forEach((file) => {
require(file)(app, passport, React, ReactDOMServer);
});
// The following routes any invalid routes black to the root page
app.get('/*', (req, res) => {
req.flash('errorMessage', 'Invalid route');
res.redirect('/');
});
// Tells server to listen to port 4000 when app is initialized
app.listen(4000, () => {
console.log('Gravity app running on port 4000');
});