体验Meteor

Meteor是什么?

Meteor官网的教程上是这样对Meteor进行解释的:

Meteor is a full-stack JavaScript platform for developing modern web and mobile applications. Meteor includes a key set of technologies for building connected-client reactive applications, a build tool, and a curated set of packages from the Node.js and general JavaScript community.

  • Meteor是一个构建在Node.js之上的全栈开发平台,用来开发实时网页程序(数据更新,页面立马重新渲染)。
  • Meteor位于程序数据库和用户界面之间,保持二者之间的数据同步更新。
  • Meteor在客户端和服务器端都使用JavaScript作为开发语言。
  • Meteor程序的代码在前后两端共用。
  • Meteor的数据库默认使用MongoDB。

Meteor存在丰富的资源:atmospherejs

Quick start

本文使用的开发环境是Windows 10,使用ReactJs开发。

创建一个简单的应用

安装Meteor

官网上下载Meteor的安装包,按照常规安装方法进行安装即可。

创建一个应用

常用命令

meteor help <command> :帮助
meteor create <app-name> : 创建项目
meteor create --list : 查看有哪些示例项目
meteor create --example <example-name> : 创建示例项目的工程。example-name为meteor create --list命令的结果。
meteor create --package <package-name> : 创建本地meteor包
meteor update :升级meteor及其组件到最新版本
meteor add <package-name> : 添加包
meteor remove <package-name> : 删除包
meteor : 使用Meteor运行当前应用
meteor mongo : 打开meteor的mongodb shell。
meteor reset : 重置项目为初始状态,移除本地所有数据。
meteor deploy : 部署程序。
meteor debug : 用debug模式运行程序。

示例

步骤1:meteor create news-list
步骤2:cd news-list
步骤3:meteor npm install
步骤4:meteor
步骤5:运行浏览器,输入http://localhost:3000查看结果

官网提供了丰富的TodoList系列例子是一个非常值得学习的范例。

还有一些比较有意思的项目: Rocket.ChatwekanTelescope等,我们可以在这些项目的基础上进行开发。

代码风格以及目录结构

代码风格

  • 建议采用统一的代码规范,可以使用eslint工具。
  • 建议使用ES2015/ES6

目录结构

文件结构

通过官方的一个例子来说明目录结构:

imports/
  startup/
    client/
      index.js                 # import client startup through a single index entry point
      routes.js                # set up all routes in the app
      useraccounts-configuration.js # configure login templates
    server/
      fixtures.js              # fill the DB with example data on startup
      index.js                 # import server startup through a single index entry point

  api/
    lists/                     # a unit of domain logic
      server/
        publications.js        # all list-related publications
        publications.tests.js  # tests for the list publications
      lists.js                 # definition of the Lists collection
      lists.tests.js           # tests for the behavior of that collection
      methods.js               # methods related to lists
      methods.tests.js         # tests for those methods

  ui/
    components/                # all reusable components in the application
                               # can be split by domain if there are many
    layouts/                   # wrapper components for behaviour and visuals
    pages/                     # entry points for rendering used by the router

client/
  main.js                      # client entry point, imports all client code

server/
  main.js                      # server entry point, imports all server code

建议将代码放到imports文件夹中,在Meteor v1.3中就可以根据需要对引用的文件进行懒加载。

  • imports/startup:将系统启动是需要进行处理的代码放在该文件夹下。
  • imports/api:将业务逻辑代码放在该文件夹下。
  • imports/ui:将UI渲染的代码放在此处。
  • client:存放在客户端运行的代码。
  • server:存放在服务端运行的代码。

加载顺序

  1. html模板文件将首先加载。
  2. 以main.开头的文件将最后加载。
  3. 在任何lib/文件夹下的文件将随后加载。
  4. 更深层的目录将随后加载。
  5. 整个路径中文件以字母顺序加载。

例子如下:

nav.html
main.html
client/lib/methods.js
client/lib/styles.js
lib/feature/styles.js
lib/collections.js
client/feature-y.js
feature-x.js
client/main.js

上述的例子是以加载顺序列出的。因为html总是被优先加载,所以main.html在第二个被加载了。因为第4条,目录比较深的client/lib/styles.js和client/lib/methods.js等会优先加载。根据第5条,client/lib/styles.js比lib/feature/styles.js优先加载。

可以通过Meteor.startup控制那些可以运行在服务端也可以运行在客户端的代码。

一些特殊的文件夹

以下是一些特殊的文件夹说明:

  • imports

    该文件夹下没有被import的文件不会被加载。

  • client

    该文件夹下的代码只会被客户端运行。

  • server

    该文件夹下的代码只会在服务端运行。

  • public

    该文件夹下中的文件会原样发送到客户端。用来放置资源等。假设有张图片/public/background.png, 在HTML中用 <img src=’/background.png’/>引用或是在CSS中用 background-image: url(/background.png) 引用。 注意图片URL中不包含/public。

  • private

    该文件夹中的文件只能由服务端代码通过Assets API 来获取,客户端无法获取。

  • client/compatibility

    这个文件夹是兼容的JavaScript库。各文件中使用var定义的变量将成为全局变量使用。该文件夹下的各文件在其他的客户端文件执行前被执行。

  • tests

    测试文件夹。