I’ve been wanting to try the MEAN stack for quite a while, but it was a little bit intimidting back then when I had nearly zero experience with Mongo, Express, Node, Angular, but heard people talk about them constantly.
Although right now I am still by no means any kind of expert in those things, I have to say this MEAN stack is fascinating since you will be dealing with code from backend to frontend all in javascript (yay).
There is a detailed wiki about this experiment, the note here is more like a introduction.
Intro
I am going to use express, which is a popular nodeJS web framework, to provide API to the front end. The express framework will communicate with the database(MongoDB) to provide and save data comes from the front end. The front end will be all AngularJS, which is also a MVW front-end framework.
So make sure you have Node and MongoDB properly installed on your laptop first.
Part 1 - Build With the Framework
In reality, the order of build front end first or back end first varies based on your team, Here I would like to build up the backend part first.
The Entry Point
Normally there would be an entry point js file for the whole project, let’s name it app.js.
This file will contain some basic configuration for the app, including the template engine, dev environment settings, etc. Just for the record, you can use the express-generator to set up an express app scaffold. But I have some modifications so let’s see what we have in this file:
The Folder Structure
Based on the settings in app.js, you should have a project looks like this:
Let’s create another folder models for our data, and a lib folder for any custom node modules.
and create a index.js in the routes folder since we’ll be using it soon.
The Handy Tools
If you’ve written node apps before, you know that if the app went wrong or you modify your files, you’ll need to restart the app again and again, fortunately there are many tools available to solve this problem.
I am using gulp with gulp-nodemon and gulp-livereload so that if I change any files in the project, the application would restart itself and also automatically refresh the browser window.
##Create views
In app.js, there is one line handlebars = require('express3-handlebars').create({defaultLayout: 'base'}), that requires us to create a folder named layouts in views folder, to put the base.handlebars file, I won’t go to the details of handlebars here, let’s just say we have the index.handlebars, about.handlebars, 404.handlebars, 500.handlebars files for starters.
Create routes
In the routes/index.js file, I am going to set up some basic routes:
Part 2 - Build up the API
Not only for AngularJS, but also for many front end pages, providing a RESTful API means conveniency, so let’s plug into the database with mongoose to provide API for AngularJS.
In this specific case, let’s create a todo list App (it’s kind of cliche, but it really is a good case for beginners I’d say).So we’ll be:
Get all todo items
POST new todo item
DELETE one todo item
MongoDB and Mongoose
To save or edit the data, we need to set up the data first, create a file named todo.js in the models folder, this will be the place where we define the todo list Schema.
Also don’t forget to start the mongdoDb first:
open the terminal with: mongod --dbpath ~/Desktop/todoDB, you can change the filepath to save data somewhere else;
Now let’s see what’s in the todo model:
App VERBs
Since the data model is ready, let’s write the app Verbs, such as ‘get’ and ‘post’ in routes/index.js:
Part 3 - Use APIs in FrontEnd with AngularJS
So far we got the API ready to use, now let’s focus on the frontend, since AngularJS is definitely another topic worth another post, I would just cover the very basics here just to make our app ready.
Now we should do the work in public folder, Let’s make sure the project structure looks like this:
->angApp.js //the entry point for AngularJS, named it angApp to differenciate with the Express app.js
->main.js //the main.js would be used with RequireJS, but this time, we don’t need to use it.
->css
->img
->js
–>Controllers
–>Directives
–>Filters
–>Services
–>libs
Get AngularJS Ready to Use
Since we are going to use the index page as the todo app page, let’s update the index.handlebars:
angApp.js
Angular is indeed suitable for single page app, so let’s see what should we add to the file so the directives in the previous html will work:
Wrap it Up
So the related repo is here, you can fork it, start the mongodb first and use gulp in the right path to start building.
Now the basic MEAN stack is done, you can add new features or modify, like add user register or login, set due date for the item etc. As I previously mentioned, any item in the MEAN stack, Mongo, Express, Angular, Node can be another big topic, so take as much time as you need to enjoy coding in JS.