路由和 MongoDB 集合代码在 Meteor 项目中应该放在哪里?

Where should Routing and MongoDB Collection code go in a Meteor project?

在我的 Meteor 项目中,我终于 "did the right thing" 并从默认的单个 .html 和 .css 和 .js(带有“.isClient”和“.isServer” 块)文件在这样的项目结构中分成单独的文件(我的项目名称是 "Scheduler"):

Scheduler
        Client (folder)
            main.html (moved the two templates from scheduler.html for the "first page" here)
            main.js (moved the .isClient code from scheduler.js here)
            Templates (folder)
                openExistingSchedule.html (contains a template; will add more later, as the project grows)
        Server (folder)
            scheduler.js (moved the .isServer code here)

但是我应该把既不属于 "client" 也不属于 "server" 的代码放在哪里?具体来说,铁路由器代码如:

Router.route('/platypus');

...和MongoDB代码如:

Playtpus = new MongoDB.Collection('platypus');

它应该放在 "public" 文件夹的 .js 文件中,还是...???

取决于您的路由器。例如,使用 kadirahq:flow-router,如果用作仅客户端路由器,则应进入 client/。有了快速渲染支持,它应该进入 lib

加载基本规则:

  • clientpublic 中的任何内容都将加载到客户端,而不是服务器。

  • serverprivate 中的任何内容将仅在服务器中加载。

  • lib 中的任何内容都将同时加载到客户端和服务器 FIRST

  • 任何其他文件夹中的任何内容都将加载到客户端和服务器。

通常,您希望将其放在 lib 中,以便它首先加载。

iirc 您正在使用 iron-router。在开始创建 REST 端点 (docs) 之前,您可以将路由器代码保留在 /client 下。您的集合定义代码 (new MongoDB...) 需要放在 /lib 下才能供客户端和服务器使用。