Meteor: Intro to Backend

Meteor is an open-source platform for building web and mobile apps. It abstracts most of the complexity of building a backend. Meteor runs on Node and Mongo, but you don't have to write a single post or get function. Meteor allows the developer to think big-picture, eliminating some annoying server code.

You should consider three things when building the backend of a Meteor app: data, access, and methods. What data do you need to store on the server, who needs to have access to it, and how should the client interact with it.

Directory Rules: Code in the Server folder will only run on the server and is not accessible to client, and vice versa. For obvious security reasons, you don't want the client to have access to more than it needs to work properly. There's a public folder where you store assets, such as image files, but we won't get into that in this post.

Variables: Variables that are defined without use 'var' are accessible to the entire project. However, due to the directory restrictions if you define a new collection in a server directory, the client can't access it. Variables defined with a 'var' are only available in the file they have been defined in. I recommend a naming convention, such as an underscore (_variable) to distinguish between your local and global variables.

Methods: Meteor allows you to define methods by creating a Meteor.method object for the functions. Variables and methods are both at the mercy of the directory rules, so bear that in mind when declaring the methods. Methods can be called by running and passing the string name of the method.

Mongo: You have access to basic database functionality, such as insert, remove, update, and find in the server and client side of Meteor. You can pass find or findOne two objects. The first is the search criteria and the second is the fields you want returned with some sorting ability. You can use find().fetch() to return an array that can be stored and indexes accessed.

Publish/subscribe: Publishing and subscribing allow you to limit what information the client and the users have access to inside the database. You cannot use these functions until you remove autopublish (meteor remove autopublish), which is the default setting on a new Meteor project. Autopublish gives all users access to all database collections.

Meteor reset: This command is your best friend in development. It clears all your data and resets your database. You will break things and this will help you.

Here is an example of how to structure your project:

What goes in the Server directory (server only):

  • Database instantiation
  • Publishing
  • Static data necessary for database instantiation that does not need to be accessed by client
  • Methods only used by server

What goes in the Collections or Library directory (runs on both client and server):

  • Methods used by client
  • Collection instantiation

Find more info in the Meteor docs. I worked on a Meteor version of Cards Against Humanity at Hack Reactor with Games, who also wrote another blog post about Meteor.