Skip to content

Instantly share code, notes, and snippets.

Created July 19, 2017 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/afd9e3f97920257203084940db1b335a to your computer and use it in GitHub Desktop.
Save anonymous/afd9e3f97920257203084940db1b335a to your computer and use it in GitHub Desktop.
Node.js: HTTP to HTTPS solution
// 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