Jose Zamudio


Full Stack Developer | Coding, Thoughts, stories and ideas.


Simple Web App - Part I - Node.js

Version en Español AQUI

Node.je

  • PART II
  • PART III
  • PART IV

In the next set of articles we are going to build a simple app using Node.js, Hapi.js, Angular.js and MongoDB.

What is Node.js?

The quick answer: Node is Javascript on the server.

Node.js works on a v8 environment, which is what Google uses as the core of Google Chrome. Node.js allows for easily building fast and scalable web apps. It uses an event-driven model that is lightweight and efficient. Node.js can be up to 93% faster than PHP!

Node.js shines in real-time web applications where both, the client and the server, can initiate communication and exchange data. This is a big difference to typical web response paradigm where the client is always the one initiating the communication.

Node.js® is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Running an application with Node.js means you cannot use a regular web hosting. We have to get a service that can run V8 in the background for you such as Heroku (we are going to use Heroku to deploy our simple app) or Digital Ocean.

Installing Node.js

We're installing Node.js for Mac, at the end of this article you will find links on how to install Node for Windows and Linux

Installing Node.js is very easy and straight forward. Go to the Node.js Downloads page and download the latest version (as of this article the current version is v0.12.0). Since Node.js is updated constantly and an ongoing project it is always good to get it from their official website.

Node.js installer

After downloading the .pkg and running it you will get the prompt to make sure you have /usr/local/bin in your path. You can check by typing the following command in Terminal:

echo $PATH  

Caution! If you do not have it in the output make sure to add it to your .bash_profile or .bashrc files with the following line:

export PATH=/user/local/bin  

To check if Node is installed simply go to Terminal and type node and you will open a node javascript shell

You can do testing within this shell by typing the following:

node \\ to open node shell...  
> var x = 4;
undefined  
> x
4  

In the first line we are openning the shell. In line 2 we assign the value 4 to variable x. Line 3 is a default node output (don't worry). In line 4 we tell Node to print out the value of x, and line 5 is the output.

There you go, you have Node.js installed and you're ready to start creating amazing web applications using one of the most used stacks in the world!

Using Node.js

For this part of the tutorial we are going to create a simple "Hello, World!" app. Once we get into the other parts of the stack we are going to create something way better!

For this "Hello, World!" example you need to create a file in your desired folder and name it hello-world.js. Inside the newly created Javascript file write the following snippet of code:

var http = require('http');

http.createServer(function (req, res) {  
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello, World!');
}).listen(3333);

console.log('Server running at http://localhost:3333/');  

In line 1 we require the http library which is a library that comes with Node.js and allows us to create a server in our computer.
Line 3 we create a server using the createServer method on the http library and pass in a function that will take in a request (req) and a response (res).
Line 4 sends a response header to the request. We don't have to worry too much about this line.
In line 5 we end the response with the text Hello, World!.
Line 6 specifies what PORT we will be listening for in the server.
The last line console logs (in Terminal) the text inside the parenthesis.

A port is like an extension in a phone number, localhost is like the main phone number and the port is the extension. There are over 60,000, yes SIXTY THOUSAND!, ports to choose from!
You can read more on the available PORTS by clicking here.

run node hello-word.js in Terminal (make sure you are inside the folder where the file is...).

The output using the browser

On your browser you can go to http://localhost:3333 and see that now you have a website created using Node.js!

If you got lost along the way, you can check and download the source code here (one file and about 6 lines of code...).

This was PART I of about 4 or 5 blog posts in which we will be building an entire app using the ME(H)AN stack (using Hapi.js instead of Express.js). I hope you enjoy it and follow the entire series of blog posts!

Follow me on:
Twitter: @soyzamudio
LinkedIn: Jose Zamudio
Github: /Soyzamudio
Website: www.josezamudio.me

Resources:
Installing Node.js in Windows
Installing Node.js in Linux