简体   繁体   English

在Windows上使用meteor.js和mongoDB

[英]Working with meteor.js and mongoDB on windows

I am taking the first steps in learning how to develop within the meteor.js framework using my Windows 10 PC. 我正在迈出第一步,学习如何使用Windows 10 PC在meteor.js框架中进行开发。

In windows, when creating a new app, the system creates a folder with separate sub-folders for client and server .js files. 在Windows中,创建新应用程序时,系统会创建一个文件夹,其中包含用于客户端和服务器.js文件的单独的子文件夹。

My question is if I define a new Mongo collection inside the server.js file, how can I access that collection from the client.js file? 我的问题是,如果我在server.js文件中定义一个新的Mongo集合,如何从client.js文件访问该集合?

what you're asking is OS-agnostic. 您要问的是与操作系统无关的。

i think you already know that files within a folder called "server" are not seen by the client, and likewise files within a folder called "client" are not seen by the server. 我认为您已经知道客户端看不到名为“服务器”的文件夹中的文件,同样,服务器看不见名为“ client”的文件夹中的文件。

Meteor will eagerly serve files outside such folders to both the client and the server (unless it's in a folder called "imports", more on that in a moment). Meteor会急切地将此类文件夹之外的文件同时提供给客户端和服务器(除非它位于名为“ imports”的文件夹中,稍后再介绍)。

so if your project is set up with top-level folders called "client" and "server", it is common to make a folder called "collections", also at the top-level, to define the collections. 因此,如果您的项目是使用名为“ client”和“ server”的顶级文件夹设置的,则通常在顶层也创建一个名为“ collections”的文件夹来定义集合。

so let's say you have a file called collections/News.js: 因此,假设您有一个名为collections / News.js的文件:

News = new Mongo.Collection('news');

when that file is served to the server, it will create that collection in Mongo. 将该文件提供给服务器后,它将在Mongo中创建该集合。 when that file is served to the client, it will create a local collection in minimongo and associate it with the real collection. 当该文件提供给客户端时,它将在minimongo中创建一个本地集合,并将其与实际集合相关联。 in both instances, "News" is a global variable you can access from anywhere. 在这两种情况下,“新闻”都是一个全局变量,您可以从任何地方访问。

so that should answer your question. 这样就可以回答您的问题。

going further, MDG is recommending a new directory structure going forward. 更进一步,MDG建议继续使用新的目录结构。 you can read about it here: https://guide.meteor.com/structure.html 您可以在这里阅读有关内容: https : //guide.meteor.com/structure.html

in short, they want us to move to a model where files are not eagerly loaded, but explicitly imported by our code. 简而言之,他们希望我们转到一个模型,在该模型中文件不会急切加载,而是由我们的代码显式导入。 during the transition period, we are meant to put our files into /imports. 在过渡期间,我们打算将文件放入/ imports。 files in there are not eagerly loaded. 那里的文件没有急切加载。

using the same example above, "News" would probably exist in its own area, as a module, in a file like this: 使用上面相同的示例,“新闻”可能以模块的形式存在于其自身区域中,如下所示:

imports/api/news/News.js 进口/api/news/News.js

const News = new Mongo.Collection('news');
export {News};

here, the file is not eagerly imported, but whatever code relies on News would have to import that module: 在这里,文件不会急切地导入,但是依赖于News的任何代码都必须导入该模块:

import {News} from '/imports/api/news/News';

that import would work in both client and server code. 该导入将在客户端和服务器代码中均有效。

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

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