Lecture 11: Node.js

Web programming

J Mwaura

Node.js

Node.js is an open-source and cross-platform JavaScript runtime environment

Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser

This allows Node.js to be very performant

A Node.js app is run in a single process, without creating a new thread for every request

Node.js

Node.js perform an I/O operation like reading from the network, accessing a database or the filesystem

Node.js handles concurrent connections with a single server without introducing the burden of managing thread concurrency

Is a server-side scripting - to produce dynamic web page

Node.js has an event-driven architecture capable of asynchronous I/O

Node.js

Node.js allows the creation of Web servers and networking tools using JavaScript and a collection of modules that handle various core functionalities

Functions;

  • File system I/O
  • Networking (DNS, HTTP, TCP, TLS/SSL, or UDP)
  • Binary data (buffers)
  • Cryptography functions
  • Data streams, and other core functions

Node.js vs PHP

Node.js is primarily used to build network programs such as Web servers

PHP functions are blocking until completion (commands only execute after previous commands finish)

Node.js functions are non-blocking (commands execute concurrently or even in parallel and use callbacks to signal completion or failure)

Technical details

Node.js is a JavaScript runtime environment that processes incoming requests in a loop, called the event loop

  1. Threading
  2. V8
  3. Package management
  4. Unified API
  5. Event loops

Threading

Node.js operates on a single-thread event loop, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections without incurring the cost of thread context switching

  • A thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system
  • An event loop is a programming construct or design pattern that waits for and dispatches events or messages in a program
  • Asynchronous I/O (also non-sequential I/O) is a form of input/output processing that permits other processing to continue before the transmission has finished
  • A context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point

V8 Engine

It is an open-source (since 2008) javaScript execution engine by Google, which was initially built for Google Chrome

V8 compiles javaScript source code to native machine code at runtime

  • Runtime is the period of time when a program is running

Package management

npm (Node Package Manager) is a package manager for the JavaScript programming language

It is the default package manager for the JavaScript runtime environment Node.js

It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry

npm is the pre-installed package manager for the Node.js server platform

Unified API

Node.js can be combined with a browser, a database that supports JSON data (e.g. Postgres, MongoDB, or CouchDB) and JSON for a unified JavaScript development stack

Event loop

Node.js registers with the operating system so the OS notifies it of connections and issues a callback

Within the Node.js runtime, each connection is a small heap allocation

Node.js uses an event loop for scalability, instead of processes or threads

Node.js exits the event loop when there are no further callbacks to be performed

Let's do this

End of Lecture 11

Web programming

That's it!

Queries about this Lesson, please send them to: jmwaura@jkuat.ac.ke

*References*

  • Google Maps; Power Tools for Maximizing the API, 2014 Evangelos Petroutsos
  • D3 Tips and Tricks; Interactive Data Visualization in a Web Browser, 2013 Malcolm Maclean
  • Interactive Data Visualization for the Web, 2013 Scott Murray
  • Web Programming with HTML5, CSS, and JavaScript, 2019 John Dean
  • Leaflet Documentation Leaflet Team
  • Google Documentation for developers Google Team
Courtesy of
Web programming