The goal of this Step by step tutorial is to get you started with developing basic to advanced applications, understanding their fundamental concepts and strictly following the best practices. The technology stack that we’ll be using will be node (node.js) + express (Framework) + mongoDB (Database) all of which are popular, fast and highly scalable.
This step by step tutorial will be fairly in-depth so you may want to get yourself a rather large mug of whatever beverage you prefer before you settle down.
Node.js is JavaScript on the server, how cool is that?
We cannot speak about Node.js without mentioning Javascript. In fact Node.js is Server-side JavaScript. All the web developers must have been in touch with JavaScript some, that because JavaScript live in browsers during a long time. The language itself was purely frontend focused and we had to use it with some backend languages such as PHP or .Net to archive meaningful functionalities. But this is just the context. Today, with node.js, JavaScript became a “complete” language you can use it in many contexts and achieve everything with it you can achieve with any other “complete” language. In fact you can now use the same language in both the front end and server side of the projects, it allows you to run JavaScript code in the backend, outside a browser.
In order to execute the JavaScript you intend to run in the backend, it needs to be interpreted and, well, executed. This is what Node.js does, by making use of Google’s V8 VM, the same runtime environment for JavaScript that Google Chrome uses.
MongoDB, the Nextgen NoSQL database system
MongoDB (from “humongous”) is a cross-platform document-oriented database system. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster. Released under a combination of the GNU Affero General Public License and the Apache License, MongoDB is free and open source software.
First developed by the software company 10gen (now MongoDB Inc.) in October 2007 as a component of a planned platform as a service product, the company shifted to an open source development model in 2009, with 10gen offering commercial support and other services. Since then, MongoDB has been adopted as backend software by a number of major websites and services, including Craigslist, eBay, Foursquare, SourceForge, and The New York Times, among others. MongoDB is the most popular NoSQL database system.
Express Framework : Get your code well organized
Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications.
Getting Started / Pre-Requisites.
- MongoDB : Installation is as simple as downloading the installer from the mongoDB site. For this tutorial I’ve been using v1.8.2 on MacOSX but any recent version should work. Once installed you can just execute ‘mongod’ to have a local instance up and running.
- Node.js : I’ll assume that you already have an installed version of node.js, otherwise do install it by visiting http://nodejs.org/. However as node is subject to a reasonably high rate of change for the purposes of this article everything has been written to run against the latest version.
- Npm : The original version of this article required various dependencies to be installed by hand from github repositories, package management has moved on within node.js since that time and we will use this to our advantage by following the instructions on how to install the npm. Once npm is installed you should be able to execute: npm search
Quick Links for Javascript Developers
The following are some of the external guide references of better implementation of javascript / node.js coding structure.
- (1) JavaScript Quick Reference Guide – A quick JavaScript reference guide for Web Developers. http://www.tutorialspoint.com/javascript/javascript_quick_guide.htm
- (2) JavaScript Built-In Useful Functions – A complete list of all the JavaScript Functions arranged in alphabetical order – JavaScript Built-In Functions - http://www.tutorialspoint.com/javascript/javascript_quick_guide.htm
- (3) JavaScript Useful Resources – A collection of JavaScript Sites, Books and Articles is given at this page.- JavaScript Useful Resources – http://www.tutorialspoint.com/javascript/javascript_resources.htm
- (4) XHTML Tutorials – A simple tutorial on XHTML, a must learn for all Web Developers. – XHTML Tutorial – http://www.tutorialspoint.com/xhtml/index.htm
- (5) CSS Tutorials – A simple tutorial on CSS, second most important weapon for Web Developers. – CSS Tutorial – http://www.tutorialspoint.com/css/index.htm
- (6) Javascript Tutorial in PDF – Download a quick Javascript tutorial in PDF format. – Javascript Tutorial in PDF – http://www.tutorialspoint.com/javascript/javascript_pdf_version.htm
- (7) Online test environment – Get your hands dirty and start testing your code online – http://www.codecademy.com/tracks/javascript
OOP – Object oriented programming in JavaScript
The object oriented programming in JavaScript is a bit different in compare to other languages such as .NET, Java or even PHP now in days. JavaScript uses a classical constructor with function prototypes to work like a class from other languages.
Note: Since you’re using the object itself as the place to store state, all references have to be prefixed with this. It’s impossible to hide any variables since everything that accessible to your methods is also publicly readable, writable, and even deletable. Also if you have a function nested inside of anything then this will change on you unless it’s explicitly passed through or preserved with a closure.
Let get started – “Hello World”
Now, we have our working environment settled, star your IDE of chose, I will be using sublime through this tutorial. You can download it from http://www.sublimetext.com/.
So let’s just jump in the cold water and write our first Node.js application.
- Start your sublime text editor, and create your first file and name it introduction.js
- Start your Node.js command prompt (It a special node js command prompt that installs with the nodejs package).
- Back to your introduction.js file and write:
-
console.log("Cool Hello World");
-
- Using the node.js command prompt navigate to your folder where you created your introduction.js file and write: node introduction.js
This should output Cool Hello World on your terminal:
Cool Hello World
As cool as our message may sound I suppose you want to do more than writing a “Cool hello world” to your command prompt. But before doing any of the fun stuff let first start by going through the basics of JavaScript and how node.js work following the best practice.
Note: Every function in Node.js is asynchronous. Therefore, everything that would normally block the thread is instead executed in the background. This is the most important thing to remember about Node.js. For example, if you are reading a file on the file system, you have to specify a call-back function that is executed when the read operation has completed.
>> Node.js / JavaScript Step by step tutorial – Chapter 2 – Closures
-
Jason