This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
101 lines (82 loc) · 2.79 KB
/
app.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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const http = require('http');
// const https = require('https');
const fs = require('fs');
const express = require('express');
const expressLayouts = require('express-ejs-layouts');
const path = require('path');
const cookieSession = require('cookie-session');
const passport = require('passport');
const mongoose = require('mongoose');
const helmet = require('helmet');
const socketio = require('./modules/socketio');
// configurations
const keys = require('./config/keys');
const passportSetup = require('./config/passport-setup');
// constants
const port = process.env.PORT || 5000;
// CA certificates
// note: this isn't needed with heroku because TLS provided by default
// const ssl_options = {
// key: fs.readFileSync(path.join(__dirname, 'ssl', 'localhost.key')),
// cert: fs.readFileSync(path.join(__dirname, 'ssl', 'localhost.crt')),
// };
// instantiate webapp
const app = express();
// set HTTP headers appropriately
app.use(helmet());
// setup template engine
app.use(expressLayouts);
app.set('view engine', 'ejs');
// body-parser middleware
// https://stackoverflow.com/a/51844327
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// set content security policy
// ref: https://www.html5rocks.com/en/tutorials/security/content-security-policy/
// app.use((req, res, next) => {
// res.setHeader("Content-Security-Policy", "script-src 'self' https://apis.google.com");
// return next;
// });
// serve static files
app.use(express.static(path.join(__dirname, './public')));
// set up session cookies
app.use(cookieSession({
name: 'session',
keys: [keys.session.cookieKey],
maxAge: 1 * 60 * 60 * 1000,
cookie: {
secure: true,
httpOnly: true,
domain: 'herokuapp.com'
}
}));
// passport middlware
app.use(passport.initialize());
app.use(passport.session());
// connect to mongodb
mongoose
.connect(keys.mongodb.dbURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(() => console.log(`[${new Date().toLocaleString()}] Connected to MongoDB...`))
.catch(err => console.log(err));
// set up routes
app.use('/', require('./routes/index'));
app.use('/auth', require('./routes/auth'));
app.use('/command', require('./routes/command'));
app.use('/dashboard', require('./routes/dashboard'));
app.use('/devices', require('./routes/devices'));
app.use('/profile', require('./routes/profile'));
app.use('/register', require('./routes/register'));
app.use('/settings', require('./routes/settings'));
// create server and websocket connection
const server = http.createServer(app);
// const server = https.createServer(ssl_options, app);
socketio(server);
// start server
server.listen(port, () => console.log(`Server is listening on port ${port}`));