Meteor/Iron路由器外部重定向

ahr*_*ren 5 javascript meteor iron-router

因此,我正在创建一个基本的虚荣URL系统,我可以http://myURL.com/v/some-text从数据库中获取项目,并根据客户端是否为移动/桌面和其他功能重定向到特定的URL.

我通常构建Facebook应用程序,因此在桌面的情况下,它们将被重定向到Facebook URL,否则在移动设备上我可以使用普通路由.

有没有办法从服务器端的Iron Router重定向到外部网站?

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('/');
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

Dav*_*don 13

这是一个使用服务器端路由的简单的基于302的重定向:

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

如果您导航到http:// localhost:3000/google/dogs,则应将您重定向到https://www.google.com/#q=dogs.

请注意,如果您想用302响应所有请求动词(GET,POST,PUT,HEAD等),您可以这样写:

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

如果您正在为SEO目的进行重定向,这可能是您想要的.