代码之家  ›  专栏  ›  技术社区  ›  Camden Narzt

如何将请求代理到websocket API?

  •  0
  • Camden Narzt  · 技术社区  · 6 年前

    我正在尝试使用 webpack-serve 为生成的前端文件(react应用程序)和代理websocket请求提供服务 /api 一个单独的过程。

    另一个进程监听端口 5000 , 网页包服务 5001 .

    1006

    我将基本身份验证层添加到 网页包服务 / 将触发浏览器请求凭据,然后将凭据发送到 /原料药 请求,因为另一个进程需要基本身份验证。

    我要知道我做错了什么。

    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'
        }
    };
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Camden Narzt    6 年前

    结果我不得不在我的 proxy()

    auth: 'admin:password', ws: true