迁移到CloudFlare后,Chrome中出现无效的重定向错误

时间:2018-12-12 15:08:08

标签: node.js google-chrome ssl amazon-ec2 cloudflare

我的网站www.exampartner.in使用与连接到我在bigrock.in上购买的域的NodeJS一起在EC2实例上运行。为了使用SSL,我迁移到CloudFlare,但是自从迁移到CloudFlare以来,我的网站就出现了问题。主要问题是将https重定向设置为页面规则后,我在javascript,徽标和ham图片上遇到无效的重定向错误。然后,我禁用了它,并尝试通过HTTP加载这些资源,但仍然失败。现在,我也禁用了SSL,该页面似乎正在运行。我已附上screenshot供参考。请帮忙。谢谢。 这是我的routes.js:

var express = require('express');
var router = express.Router();
var passport = require('passport');

router.get('/login', function (req, res, next) {
    res.render('login', { message: req.flash('loginMessage'), user: req.user });
});

router.get('/', function (req, res) {
    res.render('mainpage', { user: req.user });
});

router.get('/signup', function (req, res, next) {
    res.render('signup', { message: req.flash('signupMessage'), user: req.user });
});
// process the login form
router.post('/login', passport.authenticate('local-login', {
    successRedirect: '/profile', // redirect to the secure profile section
    failureRedirect: '/login', // redirect back to the signup page if there is an error
    failureFlash: true // allow flash messages
}));


// app.post('/signup', do all our passport stuff here);
router.post('/signup', passport.authenticate('local-signup', {
    successRedirect: '/profile', // redirect to the secure profile section
    failureRedirect: '/signup', // redirect back to the signup page if there is an error
    failureFlash: true // allow flash messages
}));
// =====================================
// PROFILE SECTION =====================
// =====================================
// we will want this protected so you have to be logged in to visit
// we will use route middleware to verify this (the isLoggedIn function)
router.get('/profile', isLoggedIn, function (req, res) {
    res.render('profile', {
        user: req.user // get the user out of session and pass to template
    });
});

// =====================================
// LOGOUT ==============================
// =====================================
router.get('/logout', function (req, res) {
    req.logout();
    res.redirect('/');
});
// };

// route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {

    // if user is authenticated in the session, carry on 
    if (req.isAuthenticated())
        return next();

    // if they aren't  logged redirect them to the home page
    res.redirect('/');
}

router.post('/login', passport.authenticate('local-login', {
    successRedirect: '/profile', // redirect to the secure profile section
    failureRedirect: '/login', // redirect back to the signup page if there is an error
    failureFlash: true // allow flash messages
}));

router.get('/bitsat', function (req, res) {
    res.render('bitsat', { user: req.user });
})

router.get('/aieee', function (req, res) {
    res.render('aieee', { user: req.user });
})

router.get('/ip', function (req, res) {
    res.render('ip', { user: req.user });
})

router.get('/jeemain', function (req, res) {
    res.render('jeemain', { user: req.user });
})

router.get('/jeeadvanced', function (req, res) {
    res.render('jeeadvanced', { user: req.user });
})

router.get('/jeemain/:year', function (req, res) {
    res.render(__dirname + '/views/jee_main/' + req.params.year, { user: req.user });
});

router.get('/jeemain/:year/:paper', function (req, res) {
    var filename = req.params.paper.substr(0, 2) + 'April' + req.params.year
    var mor = req.params.paper.indexOf('Morning')
    var eve = req.params.paper.indexOf('Evening')
    if (mor !== -1) {
        filename = filename + req.params.paper.substr(mor, req.params.paper.length)

    }
    if (eve !== -1) {
        filename = filename + req.params.paper.substr(eve, req.params.paper.length);
    }
    res.download(__dirname + '/downloads/jee_main/' + req.params.year + '/' + filename + '.pdf')
});

router.get('/bitsat/:op', function (req, res) {
    res.render(__dirname + '/views/bitsat/' + req.params.op, { user: req.user });
});

router.get('/bitsat/:op/:year', function (req, res) {
    res.download(__dirname + '/downloads/bitsat/' + req.params.op + '/' + req.params.year + '.pdf')
});



module.exports = router;

1 个答案:

答案 0 :(得分:0)

编辑:为了使用Cloudflare的严格SSL,您需要在服务器上安装SSL证书。 Lets Encrypt提供免费的SSL证书,该证书必须每90天更新一次。如果此过程似乎很困难,则可以使用Zero SSL,这使得从let加密中获取证书以及更新现有证书变得容易。

您必须设置express来处理https请求/响应。 Node.js应用需要ssl证书和密钥。

const express = require('express')
const fs = require('fs')
const https = require('https')
const app = express()



https.createServer({
  key: fs.readFileSync('server.key'),
  cert: fs.readFileSync('server.cert')
}, app)
.listen(process.env.PORT || 3000, function () {
  console.log('"App Listening at port:" + port')
})