HTTPS redirection for all routes node.js/express - Security concerns

后端 未结 3 931
遇见更好的自我
遇见更好的自我 2020-12-31 03:52

I recently took a stab at setting up HTTPS on a node/express server. I have successfully managed to redirect all the routes to use https using the code below:



        
3条回答
  •  臣服心动
    2020-12-31 04:24

    You can simply use your https_redirect function (though a bit modified) as a to automatically redirect all of your secure requests:

    // force https redirect
    var https_redirect = function () {
      return function(req, res, next) {
        if (req.secure) {
          if(env === 'development') {
            return res.redirect('https://localhost:3000' + req.url);
          } else {
            return res.redirect('https://' + req.headers.host + req.url);
          }
        } else {
          return next();
        }
      };
    };
    app.use(https_redirect());
    
    app.get('/', routeHandlerHome);
    

提交回复
热议问题