forked from davidgatti/How-to-Stream-Movies-using-NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
142 lines (116 loc) · 2.97 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
let path = require('path');
let logger = require('morgan');
let express = require('express');
let bodyParser = require('body-parser');
let app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
//
// Check for HTTPS
//
app.use(force_https);
//
// Expose the public folder to the world
//
app.use(express.static(path.join(__dirname, 'public')));
//
// Remove the information about what type of framework is the site running on
//
app.disable('x-powered-by');
//
// HTTP request logger middleware for node.js
//
app.use(logger('dev'));
//
// Parse all request as regular text, and not JSON objects
//
app.use(bodyParser.json());
//
// Parse application/x-www-form-urlencoded
//
app.use(bodyParser.urlencoded({ extended: false }));
//////////////////////////////////////////////////////////////////////////////
app.use('/', require('./routes/index'));
app.use('/video', require('./routes/video'));
//////////////////////////////////////////////////////////////////////////////
//
//
// If nonce of the above routes matches, we create an error to let the
// user know that the URL accessed doesn't match anything.
//
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
//
// Display any error that occurred during the request.
//
app.use(function(err, req, res, next) {
//
// 1. Set the basic information about the error, that is going to be
// displayed to user and developers regardless.
//
let obj_message = {
message: err.message
};
//
// 2. Check if the environment is development, and if it is we
// will display the stack-trace
//
if(process.env.NODE_ENV == 'development')
{
//
// 1. Set the variable to show the stack-trace to the developer
//
obj_message.error = err;
//
// -> Show the error in the console
//
console.error(err);
}
//
// 3. Display a default status error, or pass the one from
// the error message
//
res.status(err.status || 500);
//
// -> Show the error
//
res.json(obj_message);
});
// _ _ ______ _ _____ ______ _____ _____
// | | | | ____| | | __ \| ____| __ \ / ____|
// | |__| | |__ | | | |__) | |__ | |__) | (___
// | __ | __| | | | ___/| __| | _ / \___ \
// | | | | |____| |____| | | |____| | \ \ ____) |
// |_| |_|______|______|_| |______|_| \_\_____/
//
//
// Check if the connection is secure, if not, redirect to a secure one.
//
function force_https(req, res, next)
{
//
// 1. Redirect only in the production environment
//
if(process.env.NODE_ENV == 'production')
{
//
// 1. Check what protocol are we using
//
if(req.headers['x-forwarded-proto'] !== 'https')
{
//
// -> Redirect the user to the same URL that he requested, but
// with HTTPS instead of HTTP
//
return res.redirect('https://' + req.get('host') + req.url);
}
}
//
// 2. If the protocol is already HTTPS the, we just keep going.
//
next();
}
module.exports = app;