-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
104 lines (84 loc) · 2.8 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
104
const express = require('express');
const passport = require('passport');
const mongoose = require('mongoose');
const flash = require('connect-flash');
const session = require('express-session');
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const pug = require('pug');
const compression = require('compression');
const app = express();
const {PORT, DATABASE_URL} = require('./config/config');
mongoose.Promise = global.Promise;
// force use of https://
app.use((req, res, next) => {
let sslUrl;
if (process.env.NODE_ENV === 'production' && req.headers['x-forwarded-proto'] !== 'https') {
sslUrl = ['https://aeropress-me-app.herokuapp.com', req.url].join('');
return res.redirect(sslUrl);
}
return next();
});
// Compression for pagespeed
app.use(compression({ level: 9, threshold: 0 }));
require('./config/passport')(passport);
app.use(morgan('common'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.set('view engine', 'pug');
app.use(session({secret: 'aeropressmeappsecrets', saveUninitialized: true, resave: true}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
// 'path' variable available for Pug templates
app.use(function setLocals(req, res, next) {
res.locals.path = req.path;
next();
});
require('./routes')(app, passport);
app.use(express.static('public'));
let server;
// closeServer needs access to a server object, but that only
// gets created when `runServer` runs, so we declare `server` here
// and then assign a value to it in run
// this function connects to our database, then starts the server
function runServer() {
return new Promise((resolve, reject) => {
mongoose.connect(DATABASE_URL, err => {
if (err) {
return reject(err);
}
server = app.listen(PORT, () => {
console.log(`Your app is listening on port ${PORT}`);
resolve(PORT);
})
.on('error', err => {
mongoose.disconnect();
reject(err);
});
});
});
}
// this function closes the server, and returns a promise. we'll
// use it in our integration tests later.
function closeServer() {
return mongoose.disconnect().then(() => {
return new Promise((resolve, reject) => {
console.log('Closing server');
server.close(err => {
if (err) {
return reject(err);
}
resolve();
});
});
});
}
// if server.js is called directly (aka, with `node server.js`), this block
// runs. but we also export the runServer command so other code (for instance, test code) can start the server as needed.
if (require.main === module) {
runServer().catch(err => console.error(err));
}
module.exports = {app, runServer, closeServer};