这就是我的想法,伪代码。
const myRedirect = (routePath) => {
newUrl = routePath;
if (matches condition)
newUrl = do_some_modification(routePath);
return next(newUrl);
}
const myFunc = (routePath, myRedirect) => (newUrl, middleware) => {
return (ctx, newUrl, next) => {
return middleware(ctx, newUrl, next);
}
};
请如何修改它以使其工作?
最佳答案
const route = async function(ctx, next){
if(shouldRedirect){
ctx.redirect('/redirect-url'); // redirect to another page
return;
}
ctx.someData = getSomeData(); // ctx.someData will be available in the next middleware
await next(); // go to next middleware
}
关于koa - 如何在 koa 2 中有条件地重定向一些 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46166691/