我正在尝试重定向来自http://my域的用户,并且
https://www.my-domain
到https://my-domain
我的第一个想法是使用.htaccess文件,但据我所知,它在Nodejs上不起作用
这个函数是我在代码中重新创建.htaccess逻辑的尝试
app.use((req, res, next) => {
if (req.protocol === 'http') {
return res.redirect(301, 'https://' + req.headers.host + req.url);
}
next();
});
它确实有效,但仅在从http重定向到https时有效
并且不在此应用程序的根目录上工作(http://my-domain/)
它只适用于儿歌(http://my-domain/*至https://my-domain/*)
我能做什么?
这是app.js代码
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);