我将我的网页托管在Heroku上,并希望重定向到HTTPS。我正在使用Nodejs和Express。
我尝试了这段代码,但是当我访问页面时,发生以下错误:
我非常努力地将页面重定向到HTTPS,但无法做到这一点。感谢您的帮助!
var express = require("express");
var app = express();
var bodyParser = require('body-parser');
var nodemailer = require("nodemailer");
var favicon = require('serve-favicon');
var path = require('path');
var mongoose = require ('mongoose');
const sslRedirect = require('heroku-ssl-redirect')
app.all('/*', function (req, res, next) { // eslint-disable-line
if (/^http$/.test(req.protocol)) {
const host = req.headers.host.replace(/:[0-9]+$/g, '') // strip the port # if any
return res.redirect(301, `https://${host}${req.url}`)
}
return next()
})
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.set("view engine", "ejs");
app.get("/jobs", function(req, res){
res.render("jobs");
});
app.get("/", function(req, res){
res.render("home");
});
app.get("/galerie", function(req, res){
res.render("gallerie");
});
app.get("/team", function(req, res){
res.render("team");
});
app.get("/uns", function(req, res){
res.render("uns");
});
app.get("/contact", function(req, res) {
res.render("contact");
});
const port = process.env.PORT || 3000;
const ip = process.env.IP || "127.0.0.1";
app.listen(port,function(){
console.log("Server has started .... at port "+ port+" ip: "+ip);
});