7

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

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

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

4

1 回答 1

18
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
}
于 2017-09-18T18:08:26.737 回答