vuejs / vue-hackernews-2.0

HackerNews clone built with Vue 2.0, vue-router & vuex, with server-side rendering
MIT License
10.96k stars 2.15k forks source link

解决无法直接访问根目录(如:http://localhost:9000/)问题 #357

Open zwlijun opened 5 years ago

zwlijun commented 5 years ago

环境: Node + Express + VUE-SSR + ... 发现:在development模式下,直接访问 http://localhost:9000/ 时,会直接输出模板生成index.html文件,用production模式下访问正常。 解决:查看 webpack-dev-middleware 源码,发现如果options对象中的index没有配置或为true时会指向index.html,所以需要在setup-dev-server.js 定义webpack-dev-middleware时的options对象参数中增加一个index属性,值为 “/” 即可解决。

  // dev middleware
  const clientCompiler = webpack(clientConfig)
  const devMiddleware = require('webpack-dev-middleware')(clientCompiler, {
    publicPath: clientConfig.output.publicPath,
    noInfo: true,
    index: "/"  //指定index,解决无法直接访问根目的问题
  })
  app.use(devMiddleware)

有同种业务需要的可以参考(https://github.com/zwlijun/se.vuessr)