admin管理员组

文章数量:1278881

So I'm creating a basic vanity URL system, where I can have , grab an item from the database and redirect to a specific URL based on whether or not the client is mobile/desktop and other features.

I usually build Facebook apps, so in the case of desktop they would be redirected to a Facebook URL, otherwise on mobile I can just use normal routes.

Is there a way to redirect from Iron Router on the server-side to an external website?

this.route('vanity',{
    path: '/v/:vanity',
    data: function(){
        var vanity = Vanity.findOne({slug:this.params.vanity});

        // mobile / desktop detection

        if(vanity){
            if(mobile){
                // Redirect to vanity mobile link
            }else{
                // Redirect to vanity desktop link
            }
        }else{
            Router.go('/');
        }
    }
});

So I'm creating a basic vanity URL system, where I can have http://myURL./v/some-text, grab an item from the database and redirect to a specific URL based on whether or not the client is mobile/desktop and other features.

I usually build Facebook apps, so in the case of desktop they would be redirected to a Facebook URL, otherwise on mobile I can just use normal routes.

Is there a way to redirect from Iron Router on the server-side to an external website?

this.route('vanity',{
    path: '/v/:vanity',
    data: function(){
        var vanity = Vanity.findOne({slug:this.params.vanity});

        // mobile / desktop detection

        if(vanity){
            if(mobile){
                // Redirect to vanity mobile link
            }else{
                // Redirect to vanity desktop link
            }
        }else{
            Router.go('/');
        }
    }
});
Share Improve this question asked Jan 18, 2015 at 22:03 ahrenahren 17k5 gold badges52 silver badges70 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

Here is a simple 302-based redirect using a server-side route:

Router.route('/google/:search', {where: 'server'}).get(function() {
  this.response.writeHead(302, {
    'Location': "https://www.google./#q=" + this.params.search
  });
  this.response.end();
});

If you navigate to http://localhost:3000/google/dogs, you should be redirected to https://www.google./#q=dogs.

Note that if you would like to respond with a 302 to all request verbs (GET, POST, PUT, HEAD, etc.) you can write it like this:

Router.route('/google/:search', function() {
  this.response.writeHead(302, {
    'Location': "https://www.google./#q=" + this.params.search
  });
  this.response.end();
}, {where: 'server'});

This may be what you want if you are doing redirects for SEO purposes.

本文标签: javascriptMeteorIron Router external redirectionStack Overflow