文档位于https://github.com/koajs/static https://github.com/koajs/static我个人尝试 koa-static 的经历让我相信你可以only从应用程序的根 URL 提供文件。
例如:
app.use(serve('./some/dir/'));
鉴于上述使用serve
, 访问文件的 URL./some/dir/something.txt
将会localhost:3000/something.txt
。似乎没有办法配置我的应用程序,以便在以下位置提供相同的文件(以及同一目录中的所有其他文件)localhost:3000/static/something.txt
反而。
我是 Node 和 Koa 的新手,所以我刚刚开始深入研究这个,我可能错过了一些非常明显的东西。
我尝试使用 koa-route 来实现此目的:
app.use(route.get('/static/*'), serve(__dirname + '/some/dir'));
但经要求/static/something.txt
我遇到了以下情况:
TypeError: Cannot read property 'apply' of undefined
at Object.<anonymous> (/Users/me/example/src/node_modules/koa-route/index.js:34:18)
at GeneratorFunctionPrototype.next (native)
at onFulfilled (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:64:19)
at /Users/me/example/src/node_modules/koa/node_modules/co/index.js:53:5
at Object.co (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:49:10)
at Object.toPromise (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:117:63)
at next (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:98:29)
at onFulfilled (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:68:7)
at /Users/me/example/src/node_modules/koa/node_modules/co/index.js:53:5
at Object.co (/Users/me/example/src/node_modules/koa/node_modules/co/index.js:49:10)
要将中间件重新定位到应用程序 url 的另一部分,您可以使用koa-mount
.
'use strict';
const koa = require('koa');
const mount = require('koa-mount');
const app = koa();
app.use(function* () { this.body = 'Hello, world'; });
app.use(mount('/foo', function*() { this.body = 'Hello, foo'; }));
app.listen(3000);
curl localhost:3000
> 'Hello, world'
curl localhost:3000/foo
> 'Hello, foo'
koa-router
itself 不支持正则表达式路径或参数匹配 https://github.com/alexmingoia/koa-router/issues/127.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)