我有一个想要映射/a/b/c.js
url =>的程序/a:b:c.js
档案;
koa version:2.3.0
koa static version: 4.0.1
极简繁殖
const KOA = require('koa');
const koaStatic = require('koa-static');
staticApp = new KOA()
staticApp.use((ctx, next) => {
let path = ctx.path.split('/');
path = path.filter(segment => segment)
ctx.path = `/${path.join(':')}`;
next()
})
staticApp.use(koaStatic(__dirname))
staticApp.listen(8888);
假设当前目录有一个文件a:b:c.js
,
当我在浏览器中访问locahost:8888\a\b\c.js
时,
程序总是会出错
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): Error: Can't set headers after they are sent.
谢谢你的帮助!
答案 0 :(得分:1)
试试这个:
static QTranslator qtTr;
qtTr.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
installTranslator(&qtTr);
似乎如果你想使用一个通用函数作为中间件,你必须返回下一个函数。
答案 1 :(得分:0)
我发现为了解决这个问题,我必须在某些路由上禁用一些中间件。就我而言,它是设置 cookie 的 Passport.js 中间件,我不想在通过 /proxy 路径的请求中发生这种情况。这是我的解决方案:
const blacklistRoute = (fn, p) => async (ctx, next) => {
if (ctx.request.path.startsWith(p)) {
return next()
} else {
return fn(ctx, next)
}
}
const app = ...
app
.use(
blacklistRoute(
koaSession(passportCookieConfig, app),
'/proxy'
)
)