[英]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。 **
所以我将以下代码添加到我的项目中: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.