webpack-dev-middleware 开发环境下,react路由跳转,express提示找不到index.html?

使用了webpack-hot-middleware中间件(不知道有没关系)。

server 中摘录代码如下:

// 存内存,热加载
app.use(require("webpack-hot-middleware")(compiler));

// 静态文件,js,css等
/*app.use(express.static('build'));*/

app.get('*', (request, response) => {
  response.sendFile(path.join(__dirname, '../build/index.html'));
});

webpack.config中的

//...
entry: {
    app: path.join(commonPath.srcPath, 'index.js')
  },
  output: {
    filename: '[name].[hash].js',
    chunkFilename: 'chunk.[id].[hash:8].js',
    path: commonPath.distPath,
    publicPath: commonPath.publicPath
  },
//...

commonPath 跟webpackconfig,server在同一文件夹webpack内。

exports.commonPath = {
  publicPath: '/',  //服务器部署目录
  distPath:path.resolve(__dirname, '..', 'build'), //导出到目录
  rootPath: path.resolve(__dirname, '..'),  // 项目根目录
  srcPath: path.resolve(__dirname, '..', 'src'), // 源码目录
  libPath: path.resolve(__dirname, '..', 'node_modules'), // node_modules
  indexHTML: path.join(__dirname, '..', 'templates', 'index.tpl'),  // 入口
  favicon: path.join(__dirname, '..', 'templates', 'favicon.ico'),   //icon
  staticDir: path.join(__dirname, '..', 'static')  //静态资源
};

访问首页没问题。加上路由

Error: ENOENT: no such file or directory, stat '/Users/geekzwb/sites/项目初始化/webpack2-react-redux-saga/build/index.html'

prod 环境下项目根目录有生成build文件夹及里面的index.html,js,css等文件。

sorry,可能表述不清楚,这里是全部的文件结构~github地址。求大神们解惑~

阅读 3k
1 个回答

使用express启动,这种的我也整过,不过如果是想要从零开始初始化一个项目,推荐使用create-react-app构建最基本的项目,然后再自定义。这种的问题好处理。
我猜测你这个问题,应该是webpack打包的basePath的问题。。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题