【问题标题】:How to redirect some URL conditionally in the koa 2如何在koa 2中有条件地重定向一些URL
【发布时间】:2018-02-20 08:15:32
【问题描述】:

这就是我的想法,伪代码。

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

请问如何修改它以使其正常工作?

【问题讨论】:

    标签: koa koa2


    【解决方案1】:
    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
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 2021-08-15
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 2021-01-08
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多