Meteor 和/private 目录 [英] Meteor and the /private directory

查看:16
本文介绍了Meteor 和/private 目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用 Meteor 1.0.3 中的 /private 目录来存储和提供 pdf 文档到浏览器.

I'm using the /private directory in Meteor 1.0.3 at the moment to store and serve up pdf documents to the browser.

举个例子,我有一个这样的文件夹结构:

As an example, I have a folder structure like so:

/application-name
 /private
  /files
   /users
    /user-name
     /pdf-file1.pdf 

我有一个带有按钮点击事件的模板.在这种情况下,我调用了几次 Meteor 方法,最后调用了服务器端 Iron Router go('render-pdf') 方法.在这些 Meteor 方法中,我使用 fs node.js 来:

I have a template with a button click event. In this event I make a couple of calls to Meteor methods and finally a server side Iron Router go('render-pdf') method. In these Meteor methods I use fs node.js to:

(1) 检查 /user-name 目录是否存在,如果不存在我创建它.

(1) check if the /user-name directory exists, and if it doesn't I create it.

(2) 创建 pdf-file.pdf 文件

(2) create the pdf-file.pdf file

然后在服务器端 Iron Router go('render-pdf') 路由,再次使用 fs node.js 来:

Then in the server side Iron Router go('render-pdf') route, again using fs node.js to:

(3) 读取创建的pdf-file.pdf 和

(3) read the created pdf-file.pdf and

(4) 最终渲染到浏览器

(4) finally render it to the browser

问题出在步骤(1)中,当创建/user-name目录时,Meteor服务器重启.在步骤(2)中,Meteor 服务器再次重启.

The problem is in step (1), when creating the /user-name directory, Meteor server restarts. In step (2), again Meteor server restarts.

但最重要的是,我的代码第一次运行时,目录不存在(步骤 (1)),出现错误.

But most importantly, the first time my code runs, and the directory does not exist (step (1)), I get an error.

然后我可以再次调用按钮事件,这次是在创建目录之后,并且 pdf 呈现得很好.

I can then call the button event again, this time after the directory has been created, and the pdf is rendered fine.

错误如下:

Error: ENOENT, no such file or directory '/Users/myname/meteor/meteor-application/private/files/users/user-name/pdf-file.pdf' at Object.fs.openSync (fs.js:438:18) at Object.fs.readFileSync (fs.js:289:15) at [object Object].Router.route.name (meteor-application/both/routes.js:225:17) at boundNext (packages/iron:middleware-stack/lib/middleware_stack.js:251:1) at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108:1) at packages/meteor/dynamics_nodejs.js:121:1 at [object Object].urlencodedParser (/Users/myname/.meteor/packages/iron_router/.1.0.7.15dqor4++os+web.browser+web.cordova/npm/node_modules/body-parser/lib/types/urlencoded.js:72:36) at packages/iron:router/lib/router.js:277:1 at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1) at [object Object].hookWithOptions (packages/iron:router/lib/router.js:276:1)

可能是当我到达尝试呈现文件的步骤 (4) 时,它要么不存在,要么应用程序正在重新启动.下次我尝试时,应用程序已经重新启动并且文件存在.

It's probably that when I get to the point step (4) of trying to render the file, it either doesn't exist yet or the application is restarting. The next time I try the application has already restarted and files exist.

我的印象是 /private 目录提供了一个处理不影响执行的文件的地方应用?对我来说,这意味着在运行时我可以添加任何我想要的东西而无需重新启动应用程序.

I was under the impression that the /private directory provides a place to handle files that do not affect the execution of the application? To me this means, at runtime I can add whatever I want without the application restarting.

小历史

起初我使用 /server 目录和 ./folder-name 子目录.这就像我添加文件夹和文件时应用程序没有重新启动一样.缺点是当我使用伟大的 Meteor-up 包 (mup) 部署 Meteor 时,部署包会忽略这些文件,除非我在内部某处添加了 *.js 文件.此外,如果我在我的 EC2 实例上创建了隐藏"文件夹结构,部署将删除该目录.

Little history

At first I used the /server directory with a ./folder-name subdirectory. This worked as when I added folder and files the application didn't restart. The downside is when I deployed Meteor using the great Meteor-up package (mup), the deployment bundle ignored these files unless I added a *.js file somewhere inside. And further, if I created the 'hidden' folder structure on my EC2 instance, the deployment would remove the directory.

所以使用 /private 文件夹解决了这个问题,或者我是这么认为的.已部署的文件夹结构和资产".但是这种方法的缺点是当我向它添加资产"时,它似乎重新启动 - 即使我认为这不是应该发生的事情.

So using /private folder solved this issue, or so I thought. The folder structure and 'assets' deployed. But the downside to this approach is when I add 'assets' to it, it seems to restart -- even though I though this wasn't something that was suppose to happen.

如何在不重启 Meteor 应用程序的情况下在 /private 目录下添加资产"(以目录和文件的形式)?如果这无法完成,我如何才能在不重新启动应用程序的情况下仅在服务器端添加资产"任何地方?

How can I add 'assets' (in the form of directories and files) under the /private directory without the Meteor application restarting? If this can't be done, how can I add 'assets' anywhere only server side without the application restarting?

当我部署到生产环境时,我希望保留一些文件夹结构,例如:

When I deploy to production, I'd like some of the folder structure to stay in place, for example:

/private/files/users

应该说在那里,而

/user-name 

目录可以是动态的.我之所以提到这一点,是因为我已经阅读过如果您执行 /.directory-name,Meteor 会忽略该文件夹及其内容.但这也包括部署.

directory can be dynamic. I only mention this because I've read if you do a /.directory-name, Meteor ignores the folder and its contents. But this includes deployments as well.

仅包含在部署包中的服务器端文件夹,当我在运行时向其中添加东西"时,不会重新启动我的应用程序...

A server side only folder that gets included in the deployment bundle, and when I add 'stuff' to it at runtime, doesn't restart my application...

在我的 mup 部署中包含 /.hidden-folder 的一种方式捆绑或每次添加时都不会重新启动 /private 文件夹stuff 在运行时添加到它.

Either a way to include /.hidden-folder in my mup deployment bundle or have the /private folder not restart every time I add stuff to it at runtime.

推荐答案

为了避免:

(1) 每次部署时覆盖/删除目录结构,

(1) overwriting/removing the directory structure every time I deployed and,

(2) 每次创建目录或文件时都重新启动 Meteor 应用程序.

(2) restarting the Meteor application every time I created a directory or file.

我决定就我而言使用 Meteor 项目外部的目录结构而不是像以前那样使用内部是有意义的.

I decided in my case it just made sense to use a directory structure outside of the Meteor project instead of inside as before.

类似于 Dropbox/users/user-name 之类的东西,或者任何真正的东西.

Something like Dropbox/users/user-name, or anything really.

我现在相信/private 和/public 文件夹比其他任何东西都更适合静态内容.

I now believe that the /private and /public folders are more for static content than anything else.

我并没有真正存储那么多文件,而且其中一些文件无论如何都只是临时的,所以这种方法会阻止我,直到我转向S3.

I'm not really storing that many files yet, and some of them are only temporary anyhow, so this method will hold me over until I move to something like S3.

请注意:

(1) 您需要授予 Meteor 用户访问项目外目录的权限.

(1) You need to give your Meteor user permissions to access the outside-the-project directory.

(2) 考虑到这会占用您 OS Instance HD 上的空间.

(2) Consider that this will take up space on your OS Instance HD.

(3) 您需要使用 Node.js 进行文件系统调用.这些调用未包含在 Meteor Fibers 中,因此您可以自行进行异步/同步编程.

(3) You'll need to use Node.js for file system calls. These calls are not wrapped in Meteor Fibers, so you're on your own in terms of async/sync programming.

这篇关于Meteor 和/private 目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆