Server side routes in Iron Router and Meteor

前端 未结 1 1093
挽巷
挽巷 2020-12-16 05:07

Forward

It seems that in Meteor, we cannot call a server side route to render a file to the page without some sort of work-around from our normal workflow, from wh

1条回答
  •  天命终不由人
    2020-12-16 06:01

    Your server-side routes are running the global onBeforeAction code (it's defined in a shared directory), which breaks because the server routes are simple REST endpoints which don't understand user authentication information (i.e. Meteor.user() doesn't work). The solution is to wrap the client-specific onBeforeAction calls with Meteor.isClient or simply move that code under the client directory. For example:

    if (Meteor.isClient) {
      Router.onBeforeAction(function () {
        if (!Meteor.user() || Meteor.loggingIn())
          this.redirect('welcome.view');
        else
          this.next();
        }
        ,{except: 'welcome.view'}
      );
    
      Router.onBeforeAction(function () {
        if (Meteor.user())
          this.redirect('home.view');
        else
          this.next();
        }
        ,{only: 'welcome.view'}
      );
    }
    
    Router.route('/pdf-server', function() {
      ...
    }, {where: 'server'});
    

    0 讨论(0)
提交回复
热议问题