egg服务器如何加载vue框架工程的生产环境静态资源

Created on 28 Sep 2017  ·  5Comments  ·  Source: eggjs/egg

egg服务器如何加载vue框架工程的生产环境静态资源

Most helpful comment

  1. 你把 webpack 那边的构建路径改为 public 不就行了
  2. 不需要复制为 index.tpl,你自己 fs read 那个 html 文件,然后文本替换下输出就好了。
const fs = require('mz/fs');
const tpl = path.join(this.app.config.static.dir,  'index.html');
// 如果没变化的话可以 cache 起来,不用每次 read
this.ctx.body = yield fs.readFile(tpl, 'utf-8');

All 5 comments

前端渲染的话直接构建到 public 目录。后端的话看 egg-view-vue

发自我的 iPhone

在 2017年9月28日,10:29,seric705 notifications@github.com 写道:

egg服务器如何加载vue框架工程的生产环境静态资源


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

可以请求静态资源了,那可不可以在请求资源的时候不加上public么,我vue框架用webpack打包出来的生产环境代码都是绝对路径的,放在/static路径下的
1506580256074

egg-static 的 prefix 自己设置,但不建议。静态资源一般最好放 cdn 去,放 egg 下的话,有个 prefix 好一点,避免和 router 冲突,反正用户又看不到静态资源的地址,入口页你自己写个 router 渲染出来就好了。

我试了下面的方法可以
1.在config.default.js里将prefix设置成'/static'
2.将vue项目生产环境代码中static目录下的css、js、images等文件放到egg服务器public目录下
3.将vue项目生产环境代码中index.html复制为egg服务器view/redirect/index.tpl
这样能够渲染出页面,保留了prefix,虽然有点麻烦
1506585312042
1506585317506
1506585798893

  1. 你把 webpack 那边的构建路径改为 public 不就行了
  2. 不需要复制为 index.tpl,你自己 fs read 那个 html 文件,然后文本替换下输出就好了。
const fs = require('mz/fs');
const tpl = path.join(this.app.config.static.dir,  'index.html');
// 如果没变化的话可以 cache 起来,不用每次 read
this.ctx.body = yield fs.readFile(tpl, 'utf-8');
Was this page helpful?
0 / 5 - 0 ratings

Related issues

shaoshuai0102 picture shaoshuai0102  ·  52Comments

musicode picture musicode  ·  55Comments

popomore picture popomore  ·  70Comments

occultskyrong picture occultskyrong  ·  35Comments

popomore picture popomore  ·  59Comments