如何使用Webpack将HTTP请求转发到https?

时间:2019-07-08 11:06:06

标签: javascript node.js webpack webpack-dev-server

我有一个https web服务器的webpack配置。当http请求到达时,它仅返回ERR_CONNECTION_REFUSED。如何配置它以将所有http请求转发到https?

在package.json上,相关脚本为:

"start:staging": "webpack-dev-server --config config/webpack.server.stage.config.js --mode production --open --host 0.0.0.0 --port 443",

在webpack.server.config.js上:

module.exports = {
...
    devServer: {
        historyApiFallback: true,
        contentBase: '/',
        public: <my domain>,
        https: {
            key: key,
            cert: cert,
            ca: ca
        }  
    }
...

我尝试使用proxy选项,但无法使其正常工作。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

  

您需要第二台服务器侦听端口80并重定向请求。 –克里斯G

进行了更多调查,发现this是答案:

const express = require('express');

// Forward http requests to https
var app = express();
app.listen(80);
app.use(function(req, res, next) {
    if(!req.secure) {
        return res.redirect(['https://', req.get('Host'), req.url].join(''));
    }
    next();
});

答案 1 :(得分:0)

似乎webpack对此功能有问题。只需将其链接给会来这里https://github.com/webpack/webpack-dev-server/issues/126的任何人。