2017-09-12 88 views
0

這就是我在想的,僞代碼。如何在koa中有條件地重定向一些URL 2

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

如何修改它使其工作?

回答

0
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 
    } 
}