简化目录结构
-
build
- webpack-client-conf.js
- webpack-base-conf.js
- setup-dev-server.js
- dist
-
server
-
decorator
- router.js
-
webpack-base-conf.js
module.exports = {
entry: path.join(__dirname,'../src/entry-client.js'),
output: {
path: path.resolve(__dirname, '../dist'),
publicPath: '/dist/',
filename: '[name].js'
},
...
}
setup-dev-server.js
const webpackDevMiddleware = require('./dev-middleware') // koa webpack-dev-middleware 封装
const clientCompiler = webpack(clientConfig)
const devMiddleware = webpackDevMiddleware(clientCompiler, {
// 绑定中间件的公共路径,使用与webpack相同
publicPath: config.output.publicPath, //与webpack-base-conf相同
stats: {
colors: true,
chunks: false
},
noInfo: true,
})
app.use(devMiddleware)
router.js
import KoaRouter from 'koa-router'
import path from 'path'
import serve from 'koa-static'
export class Route {
constructor(app, apiPath) {
this.app = app
this.apiPath = apiPath
this.router = new KoaRouter()
}
init = ()=> {
const { app, router, apiPath } = this
const staticPath = '../../dist'
app.use(serve( //静态资源目录
path.join( __dirname, staticPath)
))
router.get('/*', (ctx) => {
readyPromise.then(() => {
render(ctx)
}).catch(err => {
console.log(err)
})
})
app.use(router.routes())
app.use(router.allowedMethods())
}
}
...
以上webpack编译出的文件应该输出在内存/dist/目录中,但是实际访问localhost:3002/dist/*的时候资源都是404,请问各位大佬我是哪一步出错了呢?