What is the MEAN stack?
Older Article
This article was published 13 years ago. Some information may be outdated or no longer applicable.
Over the past few weeks I’ve talked to a lot of people, and the question I kept hearing was “What is the MEAN stack?” Fair question. Here’s my understanding of it, laid out so you can get a grip on what this acronym means and how to set it up yourself.
Update
I got some great feedback from the community (appreciated). Just to be clear: the same way the LAMP stack can swap to a WAMP stack, or PHP can swap to Python, the MEAN stack can swap its parts too. You could use EmberJS or BackboneJS instead of AngularJS. The core idea stays the same though: JavaScript as the primary language across backend and frontend. Some people also prefer CouchDB over MongoDB.
MEAN stands for MongoDB, ExpressJS, AngularJS and NodeJS. It works similarly to the LAMP stack. Here’s a side-by-side comparison: TechnologyLAMPMEANServerLinux: I think this is pretty straight forward. The basic of the LAMP stack is that it’s running on a Linux distribution.NodeJS: this is a bit tricky. NodeJS is the server that runs your application, however NodeJS has to run on Linux as well. NodeJS is an event-driven I/O server-side JavaScript environment based on Google’s V8 engine.WebServerApache: most people would run their scripts on an Apache server, very commonly used.ExpressJS: is a node.js web application framework. It helps to create an MVC like application on the server side, allows users to create routes and templates as well. (It comes with a support for a great number of multiple templating languages as well, the most common one being Jade.DatabaseMySQL: the obvious choice for most people, it’s a great RDBMS, I have been using it since forever. Some people prefer to use PostgreSQL as well, there are pros and cons for against for both - those are outside the scope of this article.MongoDB: A superb No-SQL database. I wrote a lot about how/where/why to use it, have a look at some of my previous articles.LanguagePerl/PHP/Python: In the LAMP stack these are the most commonly used programming languages.AngularJS: Now this is tricky to compare with the other languages. AngularJS is framework to create one-page dynamic apps and it runs on the client side whereas the other languages in the traditional LAMP stack would run on the backend. Think about AngularJS as a great and dynamic extension to HTML. So why would someone want to use the MEAN stack?
Because everything runs on JavaScript / JSON.
One language across the whole stack makes development simpler. MongoDB stores JSON-like objects (technically BSON, Binary JSON), which you can serve from NodeJS and ExpressJS using either the native MongoDB driver or third-party tools like Mongoose. From there, it’s easy to pass a JSON object to AngularJS and stash it in the $scope. The objects stored by the backend and the objects used by the frontend share the same format. You get a self-contained storage and display pipeline, all speaking the same data language.
My recommendation: set up each tool individually first. Install NodeJS, ExpressJS and MongoDB on your development machine and poke around until you feel how they connect. Once you’re comfortable, clone a GitHub repo. The folks at Linnovate put together a solid starter pack for the MEAN stack.
If that feels like too big a jump, try this MEAN tutorial by Thinkster. (I’m actually thinking about buying the licence and building a more “European” version with football players instead of NFL players.)
There’s also a great tutorial on setting up a complete MEAN stack on Amazon EC2 that I helped my friend with. He’s done an excellent job documenting every step. His configuration adds an extra layer on top of the standard setup. I personally don’t like letting NodeJS run on port 80, so I usually stick a reverse proxy in front of it. My pick is Nginx: it’s light, reliable, and fits the scenario perfectly. It listens on port 80 and redirects everything to port 3000, where NodeJS and ExpressJS run by default. Check the tutorial out for exact steps on setting up MEAN in the cloud using Amazon EC2, including SSH key pairing, MongoDB installation and Nginx configuration.
One word of warning though: the MEAN stack is addictive. Use it at your own risk. Have fun.