Intro to Nodejs – Turing



Intro to Nodejs – Turing

0 0


class-node-intro

Intro to Node.js

On Github itechdom / class-node-intro

Intro to Nodejs

Turing

Osama Alghanmi / @itechdom

What is Nodejs

Allows you to build scalable network applications using JavaScript on the server-side.

Nodejs runs on V8 javascript runtime.

Why use NodeJS?

  • Simple
  • Allows for more control
  • Fast (specially with IO and data driven applications)
  • Non-Blocking (Asynchronous)

What could you build with Nodejs

  • Websocket Server (chat server)
  • Web services
  • Any Real-Time Data Apps

Let's create a Server

Let's take a look at how we create a server with nodejs, it's very simple.

var http = require('http');

server = http.createServer();

Listen Server!

The server should "listen" on a specific port for a connection.

var http = require('http');

server = http.createServer();

server.listen(1337);
console.log("Server is listening on 1337");

Now we have a running server

Detect a connection event

Nodejs is built around events.

Let's listen to the connection event on the server

var http = require('http');

server = http.createServer();

server.on('connection',function(req,res){
    res.end("hello world");
});

server.listen(1337, '127.0.0.1');

{{ Exercise }}

Extend the todo app to:
  • Create a server that listens on a port
  • Listen for a connection
  • initialize a count and output a number everytime there's a connection

Nodejs is Non-Blocking

What are the things that block?

  • Reads/Writes on the Database
  • Calls to other web services

  • In other web frameworks, we create a separate thread for each request.

  • Nodejs uses only one thread!

Events

An event can be, for example:

  • Incoming Connection from the client
  • Incoming Data from the client
  • Server close event.
  • event event event.

Events (Continue)

Let's look at our server, what are the events our server can have?

  • request
  • connection
  • close

https://nodejs.org/api/http.html#http_event_request

Block Node

The only way you can block your application is that you don't end the event callback.

var http = require('http');

server = http.createServer();

server.on('connection',function(req,res){

    res.writeHead("hello");
    res.write("hi there");
    console.log("If you don't call res.end node will stop the whole application");
    console.log("This is a very common mistake");

});

server.listen(1337, '127.0.0.1');

Module

Organize our code into files that can be included into each other We use "require" to load our module we have seen it in:

var http = require('http');

Our first module

Let's create a module that:

  • take the request object
  • get the headers
  • return the connection header

Module exports

var requestConnection = function(request){

    console.log(request.headers.connection);

}
module.exports = requestFormatter;

NPM

  • Package manager for Node
  • Similar to gems in ruby
  • Installed locally or globally

Installing packages

use npm install "package_name"