0

我正在尝试设置一个开发环境webpack-serve来提供生成的前端文件(一个反应应用程序),并将 websocket 请求代理/api到单独进程的路径。

另一个进程在端口监听,在5000端口webpack-serve监听5001

但是,与其他进程的连接在建立后不久(可能立即)中断(其他进程记录连接/断开但浏览器从未在开发工具的网络选项卡中显示成功连接),我1006从浏览器中的 WebSocket 连接(但没有错误消息)。

我添加了基本身份验证层,webpack-serve以便请求/会触发浏览器请求凭据,然后凭据也将发送到/api请求,因为其他进程需要基本身份验证。

我需要知道我做错了什么。

我的webpack.config.js

const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");

const convert = require('koa-connect');
const history = require('connect-history-api-fallback');
const proxy = require('http-proxy-middleware');
const auth = require('koa-basic-auth');

module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    mode: 'development',
    devtool: 'source-map',
    resolve: {
        modules: [
            path.resolve(__dirname, 'node_modules'),
            path.resolve(__dirname, 'src')
        ],
        extensions: ['.jsx','.scss','.js']
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: "bundle.css"
        })
    ],
    optimization: {
        minimizer: [
            new UglifyJsPlugin({
                cache: true,
                parallel: true,
                sourceMap: true
            }),
            new OptimizeCSSAssetsPlugin({
                cssProcessorOptions: { discardComments: { removeAll: true } },
            })
        ]
    },
    module: {
        rules: [
            {
                test: /\.(jsx?)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                }
            },
            {
                test: /\.(s?[ca]ss)$/,
                use: [{ loader: MiniCssExtractPlugin.loader, },
                      { loader: 'css-loader', },
                      { loader: 'postcss-loader', options: {
                          plugins: function () {
                              return [
                                  require('precss'),
                                  require('autoprefixer')
                              ];
                          }
                      } },
                      { loader: 'sass-loader', options: {
                          sourceMap: true
                      } }]
            }
        ]
    },
    serve: {
        content: 'dist',
        add: (app, middleware, options) => {
            app.use(convert(proxy('/api', { target: 'ws://localhost:5000' })));
            app.use(convert(history()));
            app.use(async (ctx, next) => {
                try {
                    await next();
                } catch (err) {
                    if (401 == err.status) {
                        ctx.status = 401;
                        ctx.set('WWW-Authenticate', 'Basic');
                        ctx.body = "can't haz that";
                    } else {
                        throw err;
                    }
                }
            });
            app.use(auth({ name: 'admin', pass: 'password' }));
            middleware.webpack();
            middleware.content();
        },
        clipboard: false,
        open: true,
        port: 5001,
        host: 'rrw.myhost.com'
    }
};
4

1 回答 1

1

原来我必须将以下内容添加到我的proxy()arg-object 中:

auth: 'admin:password', ws: true
于 2018-07-01T17:40:16.203 回答