Redirect http to https express.js

前端 未结 6 1998
-上瘾入骨i
-上瘾入骨i 2020-12-19 16:56

I\'m trying to reroute http (80) to https (443) in my express app. I\'m using some middle ware to do this. If i go to my https://my-example-

6条回答
  •  我在风中等你
    2020-12-19 17:07

    This should work.

    app.use(function(req,resp,next){
        if (req.headers['x-forwarded-proto'] == 'http') {
            return resp.redirect(301, 'https://' + req.headers.host + '/');
        } else {
            return next();
        }
    });
    

提交回复
热议问题