Introduction To Node - Js
Introduction To Node - Js
JavaScript
Siddharth & Swarna
What to expect ahead?
➔ Introduction
➔ Examples
➔ Event Loops
➔ What can you do with Node.js?
➔ Node.js Ecosystem
➔ When to use Node.js?
➔ When to not use Node.js?
➔ Resources to get started with Node.js
Background
➔ In Node.js, ‘js’ doesn’t mean that its solely written in JavaScript. It comprises of 40% JavaScript
and 60% C++.
Introduction - continued...
➔ It makes use of event-loops via JavaScript’s callback functionality to implement the non-
blocking I/O.
➔ Programs for Node.js are written in JavaScript but not in the same JavaScript we are use to.
There is no DOM implementation provided by Node.js, i.e. you can not do this:
node filename.js
Example 1 - Hello
world
Following code outputs Hello
followed by world after 3s.
Event loops
➔ The event loop is what allows Node.js to perform non-blocking I/O operations
➔ Event-loops are the core of event-driven programming, almost all the UI programs use event-
loops to track the user event, for example: Clicks, Ajax
Client Clients send HTTP requests to
Node.js server.
JavaScript C++
Node bindings
(socket, http, etc)
Thread Event
V8 pool loop DNS Crypto
(libeio) (libev) (c-ares) (OpenSSL)
➔ Development tooling (module management with webpack, task running and automation through
grunt or gulp, linters like eslint or jslint, etc)
➔ Desktop applications.
➔ Node.js heavily relies on modules, in previous examples require keyword loaded the http & net
modules.
➔ Creating a module is easy, just put your JavaScript code in a separate js file and include it in
your code by using keyword require, like:
➔ Libraries in Node.js are called packages and they can be installed by typing
Install MongoDB using npm, the official MongoDB driver for Node.js.
Example 5 - Twitter streaming
When to use Node.js?
➔ Node.js is good for creating streaming based real- time services, web chat applications, static file
servers etc.
➔ If you need high level concurrency and not worried about CPU-cycles.
➔ If you are great at writing JavaScript code because then you can use the same language at both
the places: server-side and client-side.
When not to use Node.js?
➔ When you are doing heavy and CPU intensive calculations on server side, because event-loops
are CPU hungry.
➔ Node’s relational database support tools are not up to the expected level when compared to other
languages.
Resources to get started