12

我在热模块重新加载时遇到 CORS 问题 - 开发服务器。我在端口上使用开发服务器,3000但应用程序是从另一个端口提供的http://localhost:52024/

这是我得到的错误(Chrome,Windows 10):

GET http://localhost:3000//sockjs-node/info?t=1502216500095 404 (Not Found)
XMLHttpRequest cannot load http://localhost:3000//sockjs-node/info?t=1502216500095. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:52024' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
[WDS] Disconnected!

实际上我得到了两个错误:第一个是由//路径中的双斜杠引起的,另一个是与 CORS 相关的错误。这是我的webpack.config.js

const webpack = require('webpack'),
    path = require('path');

module.exports = {
    entry: {
        'admin': ['webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', './src/admin/index.js']
    },
    output: {
        path: path.join(__dirname, 'admin/dist'),
        filename: '[name].js',
        publicPath: 'http://localhost:3000'
    },
    module: {
        rules: [
            { test: /\.js$/, include: path.join(__dirname, 'src'), use: ['react-hot-loader/webpack', 'babel-loader'] },
            { test: /\.css/, use: ['style-loader', 'css-loader'] },
            { test: /\.less$/, use: ['style-loader', 'css-loader', 'less-loader'] },
            { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: ['url-loader?limit=10000&mimetype=application/font-woff'] },
            { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: ['file-loader'] },
            { test: /\.(png|jpg)$/, use: ['url-loader?limit=8192'] }
        ]
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
    ],
    devtool: 'cheap-module-eval-source-map',
    devServer: {
        port: 3000,
        hot: true,
        inline: true,
        headers: {
            "Access-Control-Allow-Origin": "*"
        }
    }
};

我开始使用 webpack:

webpack-dev-server --config webpack.config.js --progress

任何想法?我正在使用webpack 3.5.1webpack-dev-server 2.7.1谢谢。

4

2 回答 2

6
于 2017-08-09T02:43:15.360 回答
1

奇怪,我现在有同样的双斜杠问题。这可能是一个 Webpack 错误,我不确定。删除对象为我修复的output.publicPath表单。modules.export我的配置是这样的:

output: {
    filename: '[name].js',
    chunkFilename: '[name].js',
    path: path.resolve(__dirname, 'dist_dev'),
    publicPath: '/'
},

您可以<base href="http://localhost:3000/">在 HTML 模板中使用而不是output.publicPath选项。看到这个答案

关于 CORS 问题,您可以尝试按照此处的建议将访问控制标头添加到开发服务器。

于 2017-08-09T03:31:48.350 回答