-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (28 loc) · 1.05 KB
/
server.js
File metadata and controls
35 lines (28 loc) · 1.05 KB
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
//
// We require the packages we need, body parser and express, and then set up body parser to accept
// JSON and URL encoded values. We then include the `routes.js` file, in which we define the API
// end-points we're going to be using, and we pass it the `app` variable. Lastly, we specify the
// port to listen to for requests. In this case, port 3000.
//
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
// protocol check, if http, redirect to https
function checkHttps(req, res, next) {
if (req.get("X-Forwarded-Proto").indexOf("https") != -1) {
console.log("https, yo");
return next();
} else {
console.log("just http");
res.redirect("https://" + req.hostname + req.url);
}
}
app.all("*", checkHttps);
//json parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var routes = require("./routes.js")(app);
var server = app.listen(3000, function() {
//port 3000 is bound externally to 80/443
console.log("Listening on port %s", server.address().port);
});