forked from tgiesinger/netflixparty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
73 lines (58 loc) · 2.16 KB
/
index.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
//////////////////////////////////////////////////////////////////////////
// Configuration //
//////////////////////////////////////////////////////////////////////////
// express
var express = require('express');
var app = express();
// request logging
var morgan = require('morgan');
app.use(morgan('short'));
// compress responses
var compression = require('compression');
app.use(compression());
// turn off unnecessary header
app.disable('x-powered-by');
// turn on strict routing
app.enable('strict routing');
// use the X-Forwarded-* headers
app.enable('trust proxy');
// template engine
app.set('view engine', 'garnet');
// favicon
var favicon = require('serve-favicon');
var path = require('path');
app.use(favicon(path.join(__dirname, 'static/favicon.ico')));
// enforce HTTPS in production
if (process.env.NODE_ENV === 'production') {
app.use(function(req, res, next) {
// start with the protocol from the request
var protocol = req.protocol.toLowerCase();
// check for a protocol from CloudFlare
if (req.headers['Cf-Visitor'] || req.headers['cf-visitor']) {
var visitor = JSON.parse(req.headers['Cf-Visitor'] || req.headers['cf-visitor']);
if (visitor['scheme']) {
protocol = visitor['scheme'].toLowerCase();
}
}
// redirect if the protocol is not HTTPS
if (protocol !== 'https') {
res.redirect(301, 'https://' + req.hostname + req.url);
return;
}
next();
});
}
//////////////////////////////////////////////////////////////////////////
// Endpoints //
//////////////////////////////////////////////////////////////////////////
// landing page
app.get('/', function(req, res) {
res.render('index.garnet');
});
//////////////////////////////////////////////////////////////////////////
// Main event loop //
//////////////////////////////////////////////////////////////////////////
// start the server
var server = app.listen(process.env.PORT || 3000, function() {
console.log('Listening on port %d.', server.address().port);
});