In this tutorial, we’ll cover the basics of how to structure an application. You won’t learn everything there is to know but you will have an easier time:
- Keeping your files and folders organised in a logical manner.
- Navigating the source code of other developer’s projects on GitHub.
I’ll include some links at the end of the post if you want to dive deeper.
1. There Are No (Strict) Rules
Meteor doesn’t enforce any style of structuring your files and folders. If you have experience with Rails, you’d know a thing or two about the Model-View-Controller approach, but Meteor doesn’t actively encourage anything similar.
There are some things to keep in mind, which is what we’ll cover through the rest of the article, but in general:
- Every project will differ based on that project’s needs.
- You have total flexibility based on the way you like to work.
But if you’re not careful, this freedom of choice can cause problems. Your projects may become difficult to manage. Therefore, it’s a good idea to:
- Start projects with the fewest possible files and folders, then let them grow organically. Don’t create some intricate structure unless you precisely know what you’re doing. Less is more, etc.
- Take inspiration from other people’s projects. Look at open-source projects on GitHub and pay attention to structural decisions made in books, tutorials, and screencasts. You can learn something from everyone.
- Be flexible. There’s no use in setting any decisions in stone. You may have certain preferences but let those preferences bend as Meteor changes and as you become a more proficient developer.
Personally, I prefer having the freedom to work how I want to work — as a beginner, it means you don’t have to think too much about structure when just playing around — but don’t let that freedom get the better of you.
2. Certain Folders Have Certain Meanings
As I’ve covered before, code in a Meteor project runs on the client (in the user’s web browser) and on the server (when the project is hosted) by default. This is a core part of Meteor’s “magic” but we have to be careful because:
- Not all code is meant to run in both places.
- When we try to run code where it shouldn’t run, we’ll get an error.
The simple fix for this is to use an isClient
conditional:
if(Meteor.isClient){
// This code will only run on the client
}
Or an isServer
conditional:
if(Meteor.isServer){
// This code will only run on the server
}