Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / Node.js

MEAN web development #1: MEAN, the what and why

4.89/5 (15 votes)
27 May 2015CPOL11 min read 31K  
The first in a series on MEAN web development.

Yes, a new blog post and a new series! I apologize for not writing a post last week, I was a bit busy. I’ll try to make it up to you by starting an awesome series on MEAN web development.
I’ve been tweeting on MEAN and related technologies on Twitter, so be sure to follow me @sanderrossel as well.

But what is MEAN and why would you want to know and use it? That’s what we’ll look at in this first episode. We’ll also set up our environment. In the following posts we’ll look at each technology involved with MEAN in more detail.

  1. MEAN web development #1: MEAN, the what and why
  2. MEAN web development #2: Node.js in the back
  3. MEAN web development #3: More Node.js
  4. MEAN web development #4: All aboard the Node.js Express!
  5. MEAN web development #5: Jade and Express
  6. MEAN web development #6: AngularJS in the front
  7. MEAN web development #7: MongoDB and Mongoose
  8. MEAN web development #8: Sockets will rock your socks!
  9. MEAN web development #9: Some last remarks

 The what

So what is MEAN? I could call you dumb for not knowing, that would be mean (and unjustified, because you’ll know within seconds), but that’s not quite the mean I’m talking about! MEAN is actually an acronym (because we love acronyms in IT) and it stands for MongoDB, Express, AngularJS and Node.js. Chances are you’ve heard of those and if you read a previous blog post of mine, Web development #8: Where to go from here, you even know a little bit about what they are (although you could’ve got that from other sources too, of course).
In short, MongoDB is a NoSQL Document Model database, Node.js is a platform that allows you to run JavaScript on/as your web server, Express is a library that simplifies working with Node.js and AngularJS is a front end framework that let’s you create Single Page Applications (SPAs).

Don’t worry, we’ll look at all of them in much more detail later. In fact, I’ve already written on MongoDB in an earlier blog post, A first look at NoSQL and MongoDB in particular. Actually I’m going to ask you to read that post in a little bit.

So here’s the deal, MongoDB is a database that doesn’t use SQL, but JavaScript, as a query language, Node.js is a platform that let’s you use JavaScript on your back end, AngularJS is a JavaScript library for your front end and Express is just a library for Node.js. So that’s JavaScript across your entire stack! That’s pretty awesome, especially when you’re a fan of JavaScript.

You’re probably going to use more than just MEAN. MEAN is just a starting point, but, of course, you’re free to add (or remove) whatever you like. Perhaps a little jQuery UI, a HTML generator like Jade,  or sockets.io, a library for working with sockets in Node.js.

The why

So why would you use MEAN over other technologies? Here’s the thing, I’m not religious about any technology and you can probably do anything you can do with MEAN with other technologies as well. SQL Server and C# would do nicely, especially when throwing in SignalR, but then of course you’d have SQL in your database, C# on your back end and JavaScript in your browser. So just being able to stick with one language (unfortunately that language is JavaScript) could be considered a pro.

Let’s look at why you’d want to use any of the MEAN letters seperately.
MongoDB is a very flexible database that looks a little like the relational databases you already know. MongoDB is schemaless though, so adding any field becomes a breeze (no production downtime!). That’s especially neat when you have huge tables with lots of data. I know adding a field to any SQL database can be quite a challenge on big tables, because your entire table has to be updated. On top of that MongoDB scales well, much better than most SQL databases. Actually let’s stop on why you’d want to use MongoDB, just read my NoSQL and MongoDB article, A first look at NoSQL and MongoDB in particular.

Node.js is really an alternative to popular web servers such as Apache or IIS (Internet Information Services). So how is Node.js different than those two (and others)? Well, Apache and IIS both listen to incoming HTTP requests using threads. So that means a number of different little ‘processes’ (the threads) are listening for incoming requests and handle them. That means that if all threads are busy no new requests can be handled at that time and your server response becomes slower.
Node.js, in contrast, uses only a single thread to listen for incoming HTTP requests. What it does is listen for an incoming request, put it on a stack, and handle it on a different thread. This process is so fast (if you do it right) that Node.js should be able to handle more concurrent connections than Apache or IIS. Of course if you mess up in Node.js you’ll be blocking the server for everyone else too.
And if you’re going to use Node.js you’re probably going to use Express as well as it really makes Node.js development easier (but you’re entirely free to do a little ‘MAN’ development, of course :-) ).

And why would you want to use AngularJS? AngularJS is a framework for creating Single Page Applications. On ‘traditional’ web pages your page needs to refresh entirely for each server request. So imagine you’re at some web shop and you’re reading product reviews. There’s ten reviews per page and you’ve just read the tenth. When you click on ‘next page’ the entire page gets refreshed. So the server sends the entire HTML, the product images (if they’re not cached by your browser), the product description, other recommended products, etc. to your browser. That’s a whole lot of information you already had! In a SPA you’d only send the ten new product reviews and replace the old ones with the new ones in your browser (for example using jQuery, or AngularJS).
So a SPA, when done well, is a lot faster and better usable than a traditional application. Of course you may use jQuery for AJAX requests and DOM manipulation, but AngularJS is a framework that was kind of built to handle these exact cases, whereas jQuery is more general. So AngularJS could be a good choice when building SPAs. Of course you can replace AngularJS with another framework if you like, perhaps Ember.js or Backbone.js, you could be doing MEEN or MEBN development (I’m really just making that up, but why not, right?).

So put that together and MEAN is just an all-JavaScript stack that’s fast, responsive and flexible. Did I mention it’s all open source, so free to use? That’s pretty awesome!

Setting up your environment

MongoDB

Let’s get to work, shall we? Let’s start with setting up MongoDB. As I mentioned I’ve already written on MongoDB in a previous post, A first look at NoSQL and MongoDB in particular. As luck has it I’ve also described how to set up MongoDB, so I suggest you read that post and especially the “Getting started with MongoDB” bit (you can skip the C# part). Little has changed since I wrote that post some months ago, but I’ll briefly discuss the differences. You can download MongoDB from the downloads page. The default install path has changed to “C:\Program Files\MongoDB\Server\3.0\bin”. You can run MongoDB through the command line or by running mongod.exe directly.
You should also install MongoVUE, as described in the blog post. I’ll be using it for this series anyway. When first starting MongoVUE you need to specify a connection. Give it any name you like, set ‘localhost’ as server and you’re good to go. Other than that I think MongoVUE is pretty self explanatory, especially when you’ve used SQL Server.

Node.js

So let’s set up Node.js next. Head over to nodejs.org and click the big green install button. It should give you the installer for your system (I’m assuming you’re on a Windows machine). You can, of course, also select an installer manually by heading over to the downloads page. Then just run the installer and make sure you also install the npm package manager. Actually, just install everything. Once it’s installed you can run any Node.js application (which is really just a JavaScript, or .js, file) from the command prompt. And while we’re on the subject I want to coin a new term CPDD, Command Prompt Driven Development (ok, that was a joke). Why are we still using command prompts in 2015!? So I’m not a fan of command prompts because it’s a lot of typing and I always forget what to type. If you’re like me, no worries, I’ve got you covered!

So while we’re at it let’s create a very simple hello world! We’ll see Node.js in action and we’ll be able to fire it up using the command prompt. So first we’ll create a folder for our ‘project’. I’ve put it in C:\dev\hellonode\ and there I’ve created a file called hellonode.js. Now put the following code in that file.

JavaScript
var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello, Node.js!');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

As you can see it’s just JavaScript. The example was taken from the Node.js homepage. So we first create an instance of the http module by invoking the require function and pass in the string (id) ‘http’. When we have our http object we invoke the createServer function and pass in a function as only parameter. The function has a request (the HTTP request) and a response as parameters. So whenever the server receives an HTTP request this function is called and we can inspect the method (GET, POST etc.) and any parameters and basically anything that was sent with the request. We’re not really interested in any of that here though since we’re just going to send the same result every time a request is made. We write HTTP code 200 (OK) and the content type text to the result header and we end the request by invoking the end function and passing it the text ‘Hello, Node.js!’.
The function createServer returns a Server object. We invoke listen on this Server object, pass in a port and IP address (localhost in the example) and at that point our server goes up and running and listens for requests.

Don’t worry if you didn’t get that. We’ll look at Node.js in much more detail in a later post. So now to get this running. Open up a command prompt and type in the following (excluding double quotes “”): “node C:\dev\hellonode\hellonode.js” and you should see that your server is running. Now browse to localhost:1337 and you should see “Hello, Node.js!” in your browser window.
Congrats, you just created your first Node.js app!

Express and npm

So next we need to install Express. We’ll do this using npm. npm is a JavaScript package manager and it’s the default package manager for Node.js. There are alternatives, like Bower, but let’s just stick to npm. So open up another command prompt (argh!). First we want to make sure npm is on the latest version. We can issue an update command with the following command “npm install npm -g”. After that move to the folder that holds your project using command “cd C:\dev\hellonode” and then use the command “npm install express” to actually install Express to your project. To uninstall express you can use the command “npm uninstall express”.
Alternatively you can create a small file called package.json and put the following JSON in it.

JavaScript
{
  "name": "hellonode",
  "version": "1.0.0"
}

Now when installing or uninstalling a package add –save at the end of your command, like so “npm install express –save” and “npm uninstall express –save” and npm will keep track of dependencies in this file. For example when installing Express your packages.json file will look as follows.

JavaScript
{
  "name": "hellonode",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.12.3"
  }
}

So to make sure it really works you can create a JavaScript file called helloexpress.js and try out the Hello world example from the Express “Hello World” example page. Try it out by running the following command from a command prompt: “node C:\devhellonode\helloexpress.js” and browsing to localhost:3000.

So that’s it! We’ve got a little bird’s eye view on MEAN and we’ve got both MongoDB and Node.js up and running and we’ve installed Express using npm. I’m not discussing AngularJS here because we’ve seen how to add front end libraries to our pages in previous blog posts, for example Web development #6: Getting interactive with JavaScript. We’ll get to setting up AngularJS anyway when I post about it.

In next posts we’ll look at MongoDB, Node.js, Express and AngularJS in much more detail and we’ll see how they work together.
I’m looking forward to reading your comments and I hope to see you back with the next installment.

Stay tuned!

The post MEAN web development #1: MEAN, the what and why appeared first on Sander's bits.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)