I came up with the idea for a web application a while back but have been procrastinating around actually building it. I initially was going to build it using my bread and butter languages and frameworks (namely C#, ASP.Net, Entity Framework etc) but I’ve been hearing about this MEAN stack and wanted to broaden my horizons in the web dev world.
I have used Linux and OSX (I’m currently mostly using a Macbook right now) before and I am used to the Bash shell and have also used MySQL before so I wasn’t coming from a completely Microsoft focused point of view.
All this stood me in good stead for jumping into the NodeJS and Angular world and getting started creating this web app on the MEAN stack.
As a predominately Microsoft focused developer in my commercial career, moving over to creating an application on the MEAN stack was a slow process and I was definitely less productive in getting the basics of my application up and running at first. However in spite of this I was pleasantly surprised at how easy the MEAN stack was to get to grips with and how abundant the information was out on the web to help you get up and running.
I wanted to write down my rough experience of someone coming from the .Net world into the MEAN stack and which technologies I struggled with and which I needed to read up on.
What is this MEAN stack?
What should I get to know?
The main thing to brush up on is using the Bash shell to get your work done. Whether this is starting your NodeJS server, connecting to MongoDB or committing your changes to GitHub make sure your comfortable at the command line.
Just like Nuget in .Net there are package managers in the MEAN world. Mainly you need to know NPM for node packages and every Node module you use will be installed with NPM.
Bower is used for installing client side frameworks into your project. I used this to install Angular, JQuery and Bootstrap.
What I did was split my projects into two. My backend services and my front end client. I used NPM for my backends libraries and Bower for my front end libraries. This seems to work well.
You will then need to learn the obvious technologies; MongoDB, Express, Angular and Node. There will also be external libraries like bcrypt that you will need to learn for encrypting and hashing passwords and sensitive data. A must for web applications.
Getting started with MongoDB
The first thing I needed to do was download MongoDB and install it on my local machine. This step probably took the longest as I needed to learn how to setup and connect to the database from my MacBook. I then needed to learn to use the Mongoose library to persists my models in the databases. Coming from a RDBMS background I had to also get my head around the document storage approach to persistence. I already had some knowledge around this so it didn’t take long.
Build an API with NodeJS
My architecture approach was going to be service based. I built an API using Express and NodeJS and this was going to serve up all my data to the client. The client was then going to just make HTTP calls to the API from my Angular controllers.
I needed to learn about routing in express and how to load and save documents to MongoDB with Mongoose.
Authentication with Passport.
My next biggest problem was how to do authentication. I wanted to provide a simple form based login option and also provide and option to login with a Facebook account. I used the Passport library for NodeJS which supports most authentication methods around today.
I followed this great article series by Scott Smith http://scottksmith.com/blog/2014/05/05/beer-locker-building-a-restful-api-with-node-crud/ that helped me get started with my CRUD and authentication pieces.
I chose to just use Basic Auth with HTTPS to start with and I integrated Facebook authentication on top to allow a user to create an account with their Facebook login details.
When the user hits the client Facebook login they are first authenticated with Facebook and my application gets back a user id and other details about the user. My front end then calls an endpoint in my API that checks whether the user has previously registered, if they have it redirects them to their dashboard. If not they are redirected to a register page where they can enter a password and an account is then created in the database for them.
This all has to be over HTTPS as with basic auth the credentials are passed with each request. The password are also hashed within the database using the bcrypt library.
Front end development with Angular.
I did a great Pluralsight course on Angular fundamentals so I was up to speed on the basics of Angular. In my client I created controllers that make HTTP calls to my REST API and my dashboard page is essentially a single page application (SPA) with various components making calls to the API. This is a basic service architecture and later means I can add OAuth authentication on my service layer and allow other clients to connect to it.
Continuous deployment, GitHub and Docker.
I wanted top get my basic application lifecycle management in place so I can version and deploy my source code into dev and production environments.
I chose to host my application on Azure as I was pretty comfortable with it and I had an account. Azure has great support for GitHub and Docker so I decided to use those to deploy my application.
First I wanted to setup my dev environment with CI. I created two GitHub repositories, one for my API project and one for my client front end. I decided to keep them as separate modules that can be deployed individually. This avoids tight coupling within my applications CI strategy.
I decided to use Azure WebSites to host my dev environment as the CI support for GitHub is excellent. I just needed to point my websites to the GitHub repository and each time I pushed a commit it would deploy the changes and even start my Node servers automatically.
For production I decided to use Docker containers. I created a Linux VM with Docker support in Azure then created a Bash build script that automatically SSHs into the VM, does a git pull to get latest code then builds and runs the docker container which stands up the Node servers.
This seems to be a good approach so far, as I build out the application I will update on the things I find that may be of interest to anyone moving to the MEAN stack.