Starting with MEAN stack

Rachna Singhal
3 min readMay 26, 2018

--

I have been working with MEAN stack for a good 4 years now. I have also trained a considerate number of people in this art. I generally have this common steps to educate the people. I just wanted to pen down to reach a bigger audience.

HELLO!!

MEAN stands for MongoDB, ExpressJS, AngularJS and NodeJS. In this I would mostly be talking about E.N part, we can dive into Angular and MongoDB in another post. Before diving into how you write code, and how you write callbacks and etc. there are some important things to note about NodeJS

  • NodeJS is a single threaded language. NodeJS uses event loops to handle the traffic. So even if you run it on a machine with multiple core, it would still use a single core.
  • NodeJS runs the code asynchronously, which means all the code in one line will be executed together.
    For eg.
    The output of the below will not be “Hey Hello Userbut would be “Hey User Hello”
Code test.js
Result

I will jump directly to a basic Node app to serve a file, as Node is predominantly used for API development and seldomly for scripting. The best and easiest way to do API development in Node is with Express framework. Express is a web framework for Node.

I am assuming here that Node is installed in every system, if not, refer installing with nvm. To install express we simply can do that with node package manager aka npm

$ npm install express

But before we do that lets install another package called express-generator, which is used to set up the basic structure of your app. Does all the folder division and give you a complete app for you to just add your APIs and start serving

$ npm install -g express-generator

The -g option will install this command line too globally. Once installed simply run

$ express <app name>

you will get a structure like this.

The file package.json has all your dependencies and app meta. To install these dependencies. Simply run
$ npm install
in the folder. This will install a lot of packages and create a new folder called node_modules which will have all the modules installed.

You now have a skeleton for a web app ready. To test just run
$ npm start
and hit http://localhost:3000 on your browser.

I will cover the best way to write Restful APIs, MVC framework in another post.

The routes folder is where you code resides, you can play around with that to understand the basics of asynchronous programming. Keywords to note and you might want to try those out is promises vs callbacks vs async package. Node is amazing to serve APIs because of its asynchronous nature but what if you want to something synchronously. I want to call the Database to get some information and then act on that information. This operation needs to be performed synchronously. This is where promises/callbacks/async package comes into picture. All provide almost the same functionality but which to use when is the mastery. I will be covering that as well in another follow up post.

I hope this was informative enough for you to get started with Node. The world is your canvas, paint it node to node.

--

--

Rachna Singhal

Programmer, Sports Enthusiast, Theatre Lover, Blogger, Traveller, bibliophile