簡體   English   中英

Iron Router和Meteor中的服務器端路由

[英]Server side routes in Iron Router and Meteor

向前

似乎在Meteor中,我們無法調用服務器端路由將文件呈現到頁面,而無需從我們正常的工作流程中進行某種處理,從我讀到的有關服務器端路由的內容。 我希望我對此錯了,並且有一種簡單的方法來實現我想要做的......

**對不起,如果這有點長,但我認為在這種情況下提供更多的背景和背景是有道理的**

軟件/版本

我正在使用最新的Iron Router 1. *和Meteor 1. *並開始,我只是使用帳戶密碼。

背景/上下文

我有一個onBeforeAction,根據用戶是否登錄,只需將用戶重定向到歡迎頁面或主頁:

兩者/ routes.js

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'}
);

在同一個文件中,兩個/ routes.js,我有一個簡單的服務器端路由,將pdf呈現給屏幕,如果我刪除onBeforeAction代碼,路由工作(pdf呈現到頁面):

Router.route('/pdf-server', function() {
  var filePath = process.env.PWD + "/server/.files/users/test.pdf";
  console.log(filePath);
  var fs = Npm.require('fs');
  var data = fs.readFileSync(filePath);
  this.response.write(data);
  this.response.end();
}, {where: 'server'});

服務器路由上拋出異常

它不是重點,但是當我將上述服務器端路由添加到文件並獲取路由/ pdf-server時,我得到一個例外,同時保持onBeforeAction代碼到位。

可以在此處找到對異常的見解: SO異常問題

解決異常

上面的SO問題中答案的主要要點是"You use Meteor.user() in your Route.onBeforeAction but it has no access to this information""your browser make a GET/POST request" side route?], "to the server it doesn't have any information regarding the user's authentication state."

根據相同的SO回答者的解決方案是"find an alternative way to authenticate the user,"方法"find an alternative way to authenticate the user," ,其中一種方法是使用"cookies'"

所以,跟進這個,我找到了另一個SO答案(與之前一樣的回答者),其中概述了設置和獲取cookie的方法: SO Cookies技術

**因此,總而言之,為了允許服務器端路由,建議我使用cookie而不是Meteor.userId()或this.userId。 **

添加了Cookie相關代碼

所以我將以下代碼添加到我的項目中:client / main.js

Deps.autorun(function() {
    if(Accounts.loginServicesConfigured() && Meteor.userId()) {
        setCookie("meteor_userid",Meteor.userId(),30);
        setCookie("meteor_logintoken",localStorage.getItem("Meteor.loginToken"),30);
    }
}); 

在我的服務器端路由中,我將路由更改為:

兩者/ routes.js

Router.route('/pdf-server', function() {
  //Parse cookies using get_cookies function from : https://stackoverflow.com/questions/3393854/get-and-set-a-single-cookie-with-node-js-http-server
  var userId = get_cookies(req)['meteor_usserid'];
  var loginToken = get_cookies(req)['meteor_logintoken'];
  var user = Meteor.users.findOne({_id:userId, "services.resume.loginTokens.token":loginToken});
  var loggedInUser = (user)?user.username : "Not logged in";

  var filePath = process.env.PWD + "/server/.files/users/test.pdf";
  console.log(filePath);
  var fs = Npm.require('fs');
  var data = fs.readFileSync(filePath);
  this.response.write(data);
  this.response.end();
}, {where: 'server'});

但是這沒有按預期工作,setCookie代碼由於某種原因無效。

我的問題

問題1:以SO Cookies技術描述的方式設置/獲取cookie對我來說似乎不起作用,這項技術是否仍然適用於'15?

問題2:使用cookie,如何根據這些cookie通知服務器認證狀態? 或者,另一種方式,如何在我的服務器端路由中添加cookie檢查“通知”服務器有關用戶的信息? 我真的可以檢查這條路線上的任何東西; 我可以拒絕任何用戶,但不知何故,服務器需要“知道”用戶登錄的權利嗎?

問題3:Cookie是最好的解決方法,還是有更簡單的方法來實現同樣的目標?

方問:我看過一些中間件用於服務器端路由的地方,例如:

WebApp.connectHandlers.stack.splice(...);

WebApp.connectHandlers.use(function(...) ...);

但是這些例子都沒有內部安全性,以這種方式使用中間件可以讓我解決我的問題嗎?

您的服務器端路由正在運行全局onBeforeAction代碼(它在共享目錄中定義),因為服務器路由是簡單的REST端點,它們不了解用戶身份驗證信息(即Meteor.user()不起作用) 。 解決方案是使用Meteor.isClient包裝特定於客戶端的onBeforeAction調用,或者只是將該代碼移動到client目錄下。 例如:

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'});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM