如何仅在使用特定路由时在 Koa 中指定静态文件夹?

How to specify a static folder in Koa only when using a specific route?

我注意到 koa 的中间件如 koa-static (https://github.com/koajs/static) 在使用特定路由时不支持服务静态文件夹,如 "dashboard",它似乎只支持服务静态全局文件夹...什么可能是应对这一挑战的最佳解决方案?

比如我可以试试:

app.use(...)
   .use(async function(ctx){
      if ('/dashboard' === ctx.path) {
        let body = await static('./build');
        return body;
      }
   });

app.use(...)
   .use(async function(ctx){
      if ('/dashboard' === ctx.path) {
        await static('./build');
      }
   });

结果就是 "Not Found".

使用koa-mount:

npm i koa-mount

const mount = require('koa-mount');

...
.use(mount('/dashboard', static('./build')));