2022 S1 - SE3040 Lecture 03
2022 S1 - SE3040 Lecture 03
frameworks
NodeJS
1
Overview
● NodeJS
● Event loop
● Use cases
● Advantages and disadvantages
● Package manager
● Require
● Let’s do coding
NodeJS
● Developed by Ryan Dahl.
● Created with the aim of creating real-time websites with push capabilities
(websockets).
● NodeJS is an open source, cross platform runtime environment for server-
side and networking applications.
● Build on V8 engine, Chrome’s JavaScript engine.
● Uses event-driven, non-blocking I/O model which makes NodeJS
lightweight and efficient.
● Ideal for data-intensive real-time applications that run across distributed
devices.
● NodeJS comes with several JavaScript libraries that help basic
programming.
● NodeJS eco-system ‘npm’ is the largest in the world for open source
libraries.
Event loop
Use cases
● Not the best platform for CPU intensive heavy computational applications.
● Ideal for building fast and scalable network applications.
● NodeJS is capable of handling a huge number of simultaneous connections
with high throughput.
● For each connection NodeJS does not spawn new Thread causing max out of
memory instead handle all in single thread using non-blocking I/O model.
● NodeJS has achieved over 1 Million concurrent connections.
● Bubbling errors up to NodeJS core event loop will cause crashing the entire
program.
Use cases
● I/O bound applications.
● Data streaming applications.
● Data intensive real-time
applications.
● JSON APIs based applications.
● Single page applications.
Advantages
● Ability to use single programming language from one end of the
application to the other end.
● NodeJS applications are easy to scale both horizontally and vertically.
● Delivers improved performance since V8 engine compile the JS code
into machine code directly.
● Performance increased via caching modules into memory after the first
use.
● Easily extensible.
● Support for common tools like unit testing.
● Well build ‘npm’ package manager and it’s large number of reusable
modules.
Disadvantages
● Even though there are number of libraries available, the actual number of
robust libraries is comparatively low.
● Not suitable for computationally intensive tasks.
● Asynchronous programming model is complex than synchronous model.
Node package manager
● Reusable NodeJS components easily available through online repository.
● Build in version and dependency management.
● Build in scripting mechanism.
● Global installations will be available throughout the system while local
installations will only be available for that particular application.
● By default all the dependencies will get installed to ‘node_modules’
directory.
● ‘package.json’ contains all information related to the NodeJS application.
The file be placed in the root directory of the application.
● ‘package.json’ will contain name, version, author, repository, required
node version, scripts and dependencies etc.
Node package manager…
● To denote the compatible version numbers npm has mechanism for
defining them;
● Less than or equal ‘<=’, greater than or equal ‘>=’.
● Approximately equivalent to ‘~’.
● Compatible with ‘^’.
● Any ‘*’.
● Any ‘1.2.x’.
● Latest ‘latest’
● There are two types of dependencies ‘devDependencies’ (development
time dependencies) and ‘dependencies’ (application runtime
dependencies).
Node require
● NodeJS follows commonJS pattern when loading modules.
● Require modules get loaded synchronously and will be cached after the
first use.
● If the file does not start with ./, ../, or / module is not a core module
NodeJS will look the dependency on the node_modules directory.
Let’s do some coding
● Hello world.
● Read file.
● Write file.
● Stream the content of a file.
● Create event and subscribe to it.
● Create a child process and message passing between two processes.
● Create a web server and listen to a port via http protocol.
● Add a separate module and use it.
● Use a promise.
13