webpack historyApiFallback无法正常工作?

时间:2016-03-24 18:30:52

标签: webpack webpack-dev-server

我有一个使用历史记录API的单页应用。所以我在配置文件中设置了historyApiFallback:true,但我仍然可以获得404任何未知文件

命令行:webpack-dev-server --config webpack.config.js --hot --inline --progress --devtool eval

配置文件:

module.exports = {
  devServer: {
    host: 'localhost',
    port: 3000,
    historyApiFallback: true
  },
  entry: {
    main: [
      './src/main'
    ]
  },
  output: {
    path: path.join(__dirname, 'build'),
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      { test: /\.js$/, loader: 'babel', exclude: /node_modules/, query: { presets: ['es2015-loose', 'stage-1'], plugins: ['transform-decorators-legacy'] } },
      { test: /\.css?$/, loader: 'style!css' },
      { test: /\.html$/, loader: 'html' },
      { test: /\.(png|gif|jpg)$/, loader: 'url?limit=8192' },
      { test: /\.woff2(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url?limit=10000&minetype=application/font-woff2' },
      { test: /\.woff(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url?limit=10000&minetype=application/font-woff' },
      { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'file' }
    ]
  }
};

它构建得很好,但如果我尝试获取一个不存在的文件,我只会得到一个404而不是index.html

$ curl -i localhost:3000/asbasas
HTTP/1.1 404 Not Found
X-Powered-By: Express
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Content-Length: 20
Date: Thu, 24 Mar 2016 18:27:33 GMT
Connection: keep-alive

Cannot GET /asbasas

Webpack版本:

$ webpack-dev-server --help
webpack-dev-server 1.14.1

那么......我做错了什么?

更新:

我在启动时收到了一条消息

404s will fallback to /index.html

但仍然......

2 个答案:

答案 0 :(得分:1)

好的,我的错误。当我自己做这个时,我进行了设置,所以如果文件有扩展名,它就不会回退到/index.html,但是我用扩展程序运行了我的测试。我自己的错误。

在我思考的整个过程中,"我如何添加该功能?"

答案 1 :(得分:0)

devServer: {
contentBase: path.join(__dirname, "public"),
publicPath: "/",
historyApiFallback: true,
compress: true,
port: 3000,
hot: true,
open: "chrome"}

它的配置在路径“/posts”中有效,但在“/posts/:slug”中无效,可能是错误的?