似乎在 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'}
);
在同一个文件 both/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 Question on Exception
上述 SO 问题的主要答案是 “您在 Route.onBeforeAction 中使用 Meteor.user() 但它无权访问此信息”
,而当 “您的浏览器发出 GET/POST 请求”
[服务器端路由?],“到服务器它没有关于用户身份验证状态的任何信息。”
根据同一个 SO 回答者的说法,解决方案是 “找到一种替代方法来验证用户”
,其中一种方法是使用 “cookies”
.
因此,在此之后,我找到了另一个 SO 答案(由与之前相同的回答者提供),其中概述了设置和获取 cookie 的方法:SO Cookies technique
** 因此,总而言之,为了允许服务器端路由,建议我使用 cookie 而不是 Meteor.userId() 或 this.userId 之类的东西。 **
所以我在我的项目中添加了以下代码: 客户端/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 代码由于某种原因无效。
Question 1: Setting/getting the cookies in the manner depicted in the SO Cookies technique doesn't seem to work for me, does this technique still work in '15?
Question 2: Using cookies, how do I inform the server of the authentication state based on these cookies? Or, another way, How does adding the cookie check in my server side route "inform" the server about the user? I could check for anything in this route really; I could reject any user, but somehow the server needs to "know" about the user logged in right?
Question 3: Is cookies the best way to go about this, or is there a simpler way to achieve the same thing?
Side Question: I've seen a few places where middle ware is used for server side routes, for example:
WebApp.connectHandlers.stack.splice(...);
WebApp.connectHandlers.use(function(...) ...);
But none of these examples had security inside, will using middle ware in this way allow me to get around my problem?
最佳答案
您的服务器端路由正在运行全局 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'});
关于javascript - Iron Router 和 Meteor 中的服务器端路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27714618/
我的代码是$scope.loadQuestions=function(){$scope.questionCount=0;$scope.questionTable=newNgTableParams({count:[]},{total:19387,getData:function($defer,params){$scope.filter.sort=params.orderBy();$scope.filter.page=params.page();return$http.get("/api/questions",{params:$scope.filter}).then(function(re
是否允许将自定义值保存在Chrome扩展的manifest.json中?因为是json,所以我想在manifest.json中保存一些信息,特别是因为ChromeAPI可以用于Opera,我需要一些区分数据。如果manifest.json中有任何自定义数据,Chrome商店会提示吗? 最佳答案 不,不允许在Chrome扩展manifest.json文件中使用自定义值。您可以引用下面给出的链接。@约翰https://developer.chrome.com/extensions/manifest您只能在manifest.json中使用
我有以下设置并且我的错误/堆栈跟踪有问题我是否遗漏了什么?package.json看起来像:{"name":"xxxxxx","main":"server.js","private":true,"scripts":{"start":"nodemon--exec./node_modules/.bin/babel-nodeserver.js"},"dependencies":{"babel":"^6.5.2","babel-cli":"^6.6.5","babel-preset-es2015":"^6.6.0","babel-preset-react":"^6.5.0","babel-pr
我尝试使用nodemailer和电子邮件模板发送电子邮件。现在我从这里exampleemailtemplates得到了例子。但是当我检查这段代码时,出现错误apromisewasrejectedwithanon-error:[objectUndefined]请帮帮我。这是我的代码varnodemailer=require('nodemailer');varEmailTemplate=require('email-templates').EmailTemplate;exports.sendOne=function(){vartemplatesDir=config.templatesDir
我正在尝试在我的应用程序中设置代码拆分/分块-通过路由,使用require.ensure。所以这是我的路线:{require.ensure([],(require)=>{cb(null,require('attendee/containers/Profile/').default)},'attendee')}}/>以下是我的webpack配置中的相关行:constPATHS={app:path.join(__dirname,'../src'),build:path.join(__dirname,'../dist'),};constcommon={entry:[PATHS.app,],o
我正在尝试构建一个将模块/文件注入(inject)客户端包的插件。入口配置可能如下所示:entry:{'main':['some-stuff'],}我想像这样使用我的插件:functionSomePlugin(options){this.entryToAppendTo=options.entryToAppendTo}...plugins:[newSomePlugin({entryToAppendTo:'main'})]在我的插件中,我想像在Webpack本身中完成一样要求该文件,例如:SomePlugin.prototype.apply=function(compiler){compi
AJAX和Masonrygrid有问题.砌体网格很简单,没有在正确的时间启动。在滚动或使用参数搜索转到特定类别之前,效果非常好。可以找到示例站点here.砌体Masonry是一个JavaScript网格布局库。通常与Bootstrap或其他未正确对齐项目的网格系统一起使用。示例:仅自举:引导和砌体:滚动时下一列将添加到旧列之上。提供与unloadedimages几乎相同的输出这使得图像重叠。这通常可以通过使用我已经包含在提供的代码中的imagesLoaded来解决。使用参数搜索时Masonry在AJAX之后没有被解雇。这意味着它根本不起作用。所以柱子在没有砌体的情况下被加载。请参阅sa
我正在尝试使用ReactJS录制音频并希望存储在我的Node服务器中。为此,我尝试使用“react-audio-recorder”模块,但我遇到了一些问题,比如在一个接一个地连续录制音频时,模块出现故障,我还尝试使用p5.js来处理音频录制,但我在配置它时遇到问题。请建议我在react(JavaScript)中录制音频并将其保存在我的Node服务器中的最佳方法。 最佳答案 react-mic项目可以处理录音。不知道服务端怎么做;我自己仍在努力。 关于javascript-ReactJS中
我只是在javascript中使用以下语句在一列中搜索特定字符串并过滤数据表中的记录。term=$(this).val();table.column(2).search(term,true,false).draw();这是我的JSON字符串:[{"userid":"2315","location":"x","details":"{\"subjects\":{\"English\":[\"meena\",\"teena\"]},\"hours\":{\"2\"}}}",{"userid":"3009","location":"y","details":"{\"subjects\":{
Stackoverflow对当前版本的帮助太大了,但目前我完全迷失了方向,因为我不知道如何解决一个问题。我将非常感谢您的任何建议。我有带参数的产品容器。它们出现在鼠标上:悬停(用css完成)。问题是如果有人点击手机,它会触发两个事件:点击和鼠标悬停。但我需要移动设备的不同行为。所以我使用了stackoverflow的解决方案,在那里我可以检查浏览器是否知道touchstart事件。在有人使用可触摸屏幕但使用鼠标之前,它工作正常。因此,该用户将单击容器,参数将显示出来,而不会重定向到产品详细信息。但是鼠标行为是错误的。我需要的是,如果有人点击图片,它会首先显示参数,第二次点击时它会重定向