Search code examples
angularjsherokuhttpsgulpgulp-connect

How to redirects http to https using gulp-connect


How to force https (redirect http to https) using gulp-connect on Heroku?

I've already configured SSL on Heroku and my website works great if I prefix https:// before the domain.

gulpfile.js

'use strict';

var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var history = require('connect-history-api-fallback');

gulp.paths = {
  src: 'src',
  dist: 'dist',
  tmp: '.tmp',
  e2e: 'e2e'
};

require('require-dir')('./gulp');

gulp.task('default', ['clean'], function () {
  gulp.start('build');
});

gulp.task("serve:production", function(){
  $.connect.server({
    root: './dist',
    port: process.env.PORT || 3004,
    middleware: function(connect, opt) {
      return [
        history({})
      ]
    }
  });
});

Solution

  • I've used this package before with success. It's very simple to configure.

    https://www.npmjs.com/package/express-sslify

    var express = require('express');
    var http = require('http');
    var enforce = require('express-sslify');
    
    var app = express();
    
    // Use enforce.HTTPS({ trustProtoHeader: true }) in case you are behind 
    // a load balancer (e.g. Heroku). See further comments below 
    app.use(enforce.HTTPS());
    
    http.createServer(app).listen(app.get('port'), function() {
        console.log('Express server listening on port ' + app.get('port'));
    });