如何在koa 2中有条件地重定向某些URL

时间:2017-09-12 01:47:24

标签: koa koa2

这就是我所想的伪代码。

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);
    }
};

如何对其进行修改以使其正常工作?

1 个答案:

答案 0 :(得分:1)

const route = async function(ctx, next){
    if(shouldRedirect){
        ctx.redirect('/redirect-url') // redirect to another page
    }
    else{
        ctx.someData = getSomeData() // ctx.someData will be available in the next middleware
        await next()  // go to next middleware
    }
}