Handling redirects with Koa


koa nodejs redirect Tutorials

Stay updated with my tutorials. Also, I will delight you with some bonus content.

In Koa.js framework, you can make redirect using the .redirect() method of context object:

ctx.redirect('https://www.nytimes.com/');

The status of this redirect will be 302:

302 redirect in Koa

A 301 redirect you can create using the next way:

ctx.status=301;
ctx.redirect('https://www.nytimes.com/');

301 redirect in Koa framework

You can pass to method the next values:

  • absolute path
  • absolute URL
  • relative path
  • ..
ctx.redirect('/some-path');
ctx.redirect('https://some-path.com');
ctx.redirect('some-path');
ctx.redirect('..');
comments powered by Disqus