关于koa-static-cache这些文件缓存的问题

我没有加载koa-static-cache这个中间件,渲染图片会不出来。请问为啥要有静态资源服务器,没有的话图片路径有问题

还有下面这张图片这个文件缓存为什幺要分成两个目录来写,单独写一个还不行。文件路径是public文件夹下面有个images文件夹

图片描述

阅读 8.5k
2 个回答

https://www.npmjs.com/package...
如果你是要访问图片的话,不应该用koa-static-cache,而是用koa-static。

项目根目录下新建 public/images

const static = require('koa-static');    //静态资源服务
const  staticPath = './public';  // 配置静态资源文件根目录
app.use(static(
    path.join( __dirname, staticPath)
));

这样子你访问图片 public/images/1.jpg 是这样子的 localhost:8080/images/1.jpg

请看官方 README.md 开头所写的内容。

Static server for koa.

Differences between this library and other libraries such as static:

* There is no directory or index.html support.
* You may optionally store the data in memory - it streams by default.
* Caches the assets on initialization - you need to restart the process to update the assets.(can turn off with options.preload = false)
* Uses MD5 hash sum as an ETag.
* Uses .gz files if present on disk, like nginx gzip_static module

因此,你这里显然应该使用 koa-static 而不是 koa-static-cache。

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