简体   繁体   English

流星中间件和服务器端路由

[英]Meteor middleware and Server side routes

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 what I've read about server side routes. 看来,在Meteor中,如果没有正常工作流程(根据我对服务器端路由的了解)进行某种变通,就无法调用服务器端路由将文件呈现到页面。 I hope I'm wrong about this, and there's a simple way to achieve what I'm looking to do... 我希望我对此是错误的,并且有一种简单的方法可以实现我想要做的事情...

Software/Versions 软件/版本

I'm using the latest Iron Router 1.* and Meteor 1.* and to begin, I'm just using accounts-password. 我使用的是最新的Iron Router 1. *和Meteor 1. *,首先,我使用的是Accounts-password。

Background/Context 背景/上下文

I have an onBeforeAction that simply redirects the user to either the welcome page or home page base upon if the user is logged in or not: 我有一个onBeforeAction,可以根据用户是否登录简单地将用户重定向到欢迎页面或主页:

both/routes.js 两者/ 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'}
);

In the same file, both/routes.js, I have a simple server side route that renders a pdf to the screen, and if I remove the onBeforeAction code, the route works (the pdf renders to the page): 在同一个文件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'});

Exception thrown on Server Route 服务器路由引发异常

It's beside the point, but I get an exception when I add the above server side route to the file and take the route /pdf-server, while keeping the onBeforeAction code in place. 这很重要,但是当我将上面的服务器端路由添加到文件中并采用路由/ pdf-server时,却出现了一个异常,同时保留了onBeforeAction代码。

Insights into the exception can be found here: SO Question on Exception 对异常的见解可以在这里找到: 关于异常的问题

My Question 我的问题

Question 4: I've seen a few places where middle ware is used for server side routes, for example: 问题4:我看过一些地方将中间件用于服务器端路由,例如:

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? 但是这些示例都没有内部安全性,以这种方式使用中间件是否可以解决我的问题?

Note: this question is a subset of a larger SO question found here , but I thought I'd break this out as the topic really deserves its own question IMO. 注意:该问题是此处找到的一个较大的SO问题的子集,但我认为我将对此进行细分,因为该主题确实值得其自己的问题IMO。

@David Weldon answered the part of the question, how to render the server side route: Server side routes in Iron Router and Meteor . @David Weldon回答了问题的一部分,即如何呈现服务器端路由: Iron Router和Meteor中的服务器端路由 Middleware doesn't solve the other half, which is authentication. 中间件不能解决另一半,即身份验证。 A SO question has been posted about that here: Authentication on Server side routes in Meteor 关于此的一个SO问题已经发布: Meteor中服务器端路由上的身份验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM