//

NODE.JS INTRODUCTION

Node.js allows you to build scalable network applications using JavaScript on the server-side. It uses the V8 JavaScript Runtime. It's written in C which makes it fast.

What could you build with Node.js?

  • Websocket Server (eg: chat server)
  • File Upload Client
  • Ad Server
  • Any Real-Time Data Apps

What is Node.js Not?

  • Not a Web Framework
  • It's not Multi-threaded. It's more of a single-threaded server.
  • A Web Framework

Hello Node!

Let's start with a simple Hello server. First, tell the response which status it should have (a successful status is 200). Next, write a message to the response body in the form of Hello, this is <your name here>. To finish it up, tell the response to end so the client on the other side knows it's received all the data.

var http = require('http');

http.createServer(function(request, response) {
  response.writeHead(200);
  response.write('Hello, this is Sean');
  response.end();
}).listen(8080);

Blocking Vs Non-Blocking Code

One of the biggest advantages of Node.js is the ability to use Non-blocking code. Let's write some blocking code followed by its non-blocking version.

Blocking Code

var fs = require('fs');
var contents = fs.readFileSync('/etc/hosts');
console.log(contents);
console.log('Doing something else');

Here it stops the process until the read action is complete.

Non-Blocking Code

fs.readFile('/etc/hosts', function(err, contents) {
    console.log(contents);
});
console.log('Doing something else');

We can move our read action into a function:

var callback = function(err, contents) {
    console.log(contents);
}
fs.readFile('/etc/hosts', callback);

Running your Code

Install Node.js in your website. Now go ahead and run that file we just created to read a file of the filesystem with node app.js

node app.js

Read File in Server

Now that you know how to create an HTTP server and how to read a file of the filesystem in a non-blocking way, let's try to combine the two. Instead of just writing a string to the HTTP response, use fs.readFile to write the contents of index.html to the response instead. Note: since fs.readFile is non-blocking, make sure you end the response inside readFile's callback.

var http = require('http');
var fs = require('fs');
http.createServer(function(request, response) {
  response.writeHead(200);
  fs.readFile('index.html', function(err, contens) {
    response.write(contens);
    response.end();
  });
}).listen(8080);

Issuing a Request

Let's see our new server in action. We've already run node app.js, so in the terminal below use curl to issue a request to http://localhost:8080 and we'll see our server respond with the contents of index.html

curl http://localhost:8080

Response WriteHead

Up until now all we've been sending into the writeHead function is the status code. However, it can take additional parameters. Let's add 'Content-Type' of 'text/html' to the response.

var http = require('http');
var fs = require('fs');

http.createServer(function(request, response) {
  response.writeHead(200, {'Content-Type': 'text/html'} );

  fs.readFile('index.html', function(err, contents) {
    response.write(contents);
    response.end();
  });
  
}).listen(8080);

Response End

Our original Hello server can be shortened since the response.end() function optionally takes data as a parameter. Remove the 'response.write' line altogether, and send the hello string as a parameter on the response.end function. This will send the data, and once finished add the end to the response.

var http = require('http');

http.createServer(function(request, response) {
  response.writeHead(200);
  response.end("Hello, this is dog");
}).listen(8080);

The Event Loop

The event loop manages events by processing one at a time from the Event Queue.