Created
July 19, 2017 21:22
-
-
Save anonymous/afd9e3f97920257203084940db1b335a to your computer and use it in GitHub Desktop.
Node.js: HTTP to HTTPS solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set up ====================================================================== | |
var express = require('express'); | |
var app = express(); // create our app w/ express | |
var bodyParser = require('body-parser'); | |
var port = process.env.PORT || 8080; // set the port | |
// configuration =============================================================== | |
app.enable('trust proxy'); //needed if you're behind a load balancer | |
app.use(function(req, res, next) { | |
if (req.secure){ | |
return next(); | |
} | |
res.redirect("https://" + req.headers.host + req.url); | |
}); | |
app.use(bodyParser.json()); // parse application/json | |
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static(__dirname + '/dist')); | |
// routes ====================================================================== | |
require('./routes.js')(app); | |
// listen (start app with node server.js) ====================================== | |
app.listen(port); | |
console.log("App listening on port " + port); | |
exports = module.exports = app; // expose app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment