Block HTTP requests in Heroku. Only only HTTPS. (No redirection)

PHOTO EMBED

Sat Sep 26 2020 11:03:32 GMT+0000 (Coordinated Universal Time)

Saved by @mishka #nodejs #express #heroku

var express = require('express');
var app = express();

app.enable('trust proxy'); //to detect if req.secure is true/false

//Block http requests. Only allow https requests
app.use(function (req, res, next) {
	if (req.headers['x-forwarded-proto'] !== 'https'){
      return res.status(404).send('Not found');
    } else {
    next();
    }
})

exports = module.exports = app;
content_copyCOPY

Used this for blocking HTTP requests in REST API routes.