How to do a meteor iron router server side redirect?
Asked Answered
K

1

8

Given something like

Router.route('/blah/:stuff', function () {
  // respond with a redirect
}, {where: 'server'});

How to do the redirect? is there something built in? or do I have to craft it myself?

This is using Meteor 1.0 / Iron Router 1.0

Kristie answered 29/10, 2014 at 9:9 Comment(0)
W
13

In server routes, you can access node's response object. Given your example, a 302 redirect could look like this:

Router.route('/blah/:stuff', function () {

  var redirectUrl = 'http://example.org/' + this.params.stuff;

  this.response.writeHead(302, {
    'Location': redirectUrl
  });

  this.response.end();

}, {where: 'server'});
Woodford answered 29/10, 2014 at 9:44 Comment(1)
@SimoneM It's up to the client to re-POST to the proper URI. HTTP 301 or 302 are only meant to inform the browser that there is a new URI to use.Schertz

© 2022 - 2024 — McMap. All rights reserved.