Im trying to redirect user from http//my-domain and https://www.my-domain to https://my-domain
my first idea was to use .htaccess file but, as i understand it doesn’t work on Nodejs
This function was my attempt to recreate .htaccess logic in code
app.use((req, res, next) => {
if (req.protocol === 'http') {
return res.redirect(301, 'https://' + req.headers.host + req.url);
}
next();
});
it does work but only in redirecting from http to https
and does not work on the root dir this application (http://my-domain/)
it only works on child dirs (http://my-domain/* to https://my-domain/*)
what can i do?
this is the app.js code
const express = require("express");
const
https = require("https"),
http = require('http'),
fs = require("fs");
var https_options = {
***
] };
var path = require("path");
const filePath = "temp.html"
var nodemailer = require('nodemailer');
require('dotenv').config();
const app = express();
const jsonParser = express.urlencoded({extended: false});
app.use(express.static(__dirname + "/app/public/public"));
app.use((req, res, next) => {
if (req.protocol === 'http') {
return res.redirect(301, 'https://' + req.headers.host + req.url);
}
next();
});
app.get("/app/public/public/styles/style.css", function(req, res){
fs.readFile(filePath, function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
});
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/app/public/public/index.html'));
});
app.get('/solutionA', function (req, res) {
res.sendFile(path.join(__dirname + '/app/public/public/shop-single.html'));
});
app.get('/solutionB', function (req, res) {
res.sendFile(path.join(__dirname + '/app/public/public/shop-item2.html'));
});
app.get('/solutionC', function (req, res) {
res.sendFile(path.join(__dirname + '/app/public/public/shop-item3.html'));
});
app.get('/solutionD', function (req, res) {
res.sendFile(path.join(__dirname + '/app/public/public/shop-item4.html'));
});
app.get('/solutionE', function (req, res) {
res.sendFile(path.join(__dirname + '/app/public/public/shop-item5.html'));
});
app.get('/solutionF', function (req, res) {
res.sendFile(path.join(__dirname + '/app/public/public/shop-item6.html'));
});
app.get('/Constructor', function (req, res) {
res.sendFile(path.join(__dirname + '/app/public/public/construct.html'));
});
app.post("/sendMail", jsonParser, function (request, res) {
console.log(request.body);
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.Mail_From,
pass: process.env.Mail_F_pas
}
});
var mailOptions = {
***
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
res.redirect(`/`);
});
app.post("/sendMailShort", jsonParser, function (request, res) {
console.log(request.body);
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.Mail_From,
pass: process.env.Mail_F_pas
}
});
var mailOptions = {
***
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
res.redirect(`/`);
});
http.createServer(app).listen(80);
https.createServer(https_options, app).listen(443);
>Solution :
Express runs middleware in order.
The reason it doesn’t work when /
is requested is because express.static
(presumably) finds an index.html
to serve which ends the request sequence.
It never moves on to your redirect middleware which will only run if express.status
doesn’t find anything.
Change the order of your middleware.