Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect some URL conditionally in the koa 2

Tags:

koa

koa2

This is what i'm thinking, pseudo code.

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

How to modify it to make it work please ?

like image 204
user3552178 Avatar asked Sep 12 '17 01:09

user3552178


Video Answer


1 Answers

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
}
like image 154
Valera Avatar answered Oct 17 '22 08:10

Valera