0% found this document useful (0 votes)
18 views35 pages

Lecture 3

The document provides an overview of Node.js, a popular open-source JavaScript runtime environment that allows developers to build scalable applications using a single-threaded, non-blocking model. It discusses the advantages of Node.js, its history, and its integration with the MEAN stack, as well as the differences between traditional web server models and Node.js's process model. Additionally, it covers the installation of Node.js, the concept of modules, and the usage of core and local modules in application development.

Uploaded by

ericcostermboya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views35 pages

Lecture 3

The document provides an overview of Node.js, a popular open-source JavaScript runtime environment that allows developers to build scalable applications using a single-threaded, non-blocking model. It discusses the advantages of Node.js, its history, and its integration with the MEAN stack, as well as the differences between traditional web server models and Node.js's process model. Additionally, it covers the installation of Node.js, the concept of modules, and the usage of core and local modules in application development.

Uploaded by

ericcostermboya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

CP 323: Web Frameworks

Development Using JavaScript


Node JS
Mean Stack Development
● MEAN, a free, open-source, full-stack
solution for MEAN applications.
● MEAN combines MongoDB, Express.js,
AngularJS, and Node.js into a single, full
stack solution for JavaScript development.
Node.js

● Node.js is an open-source and


cross-platform JavaScript runtime
environment.
● It is a popular tool for almost any
kind of project.
Node.js

● Node.js runs the V8 JavaScript


engine, the core of Google Chrome,
outside of the browser.
● This allows Node.js to be very
performant
Node.js

● A Node.js app runs in a single process,


without creating a new thread for every
request.
● Node.js provides a set of asynchronous
I/O primitives in its standard library that
prevent JavaScript code from blocking
Node.js
● Generally, libraries in Node.js are written using
non-blocking paradigms, making blocking behavior
the exception rather than the norm.
● When Node.js performs an I/O operation, like
reading from the network, accessing a database or
the filesystem, instead of blocking the thread and
wasting CPU cycles waiting, Node.js will resume the
operations when the response comes back.
Node.js
● This allows Node.js to handle thousands of
concurrent connections with a single server
without introducing the burden of managing
thread concurrency, which could be a
significant source of bugs.
Node.js
● Node.js has a unique advantage because
millions of frontend developers that write
JavaScript for the browser are now able to
write the server-side code in addition to the
client-side code without the need to learn a
completely different language.
A brief history of Node.js
● Node.js was created by Ryan Dahl in 2009.
● Ryan was inspired by the need for a way to
handle concurrent connections more
efficiently in web servers, particularly for
I/O-heavy applications.
A brief history of Node.js
● Before Node.js, JavaScript was mainly used for
client-side development in web browsers. Dahl
wanted to use JavaScript on the server-side to build
scalable, high-performance applications
● The traditional method of handling requests in web
servers was not optimal for applications with high
levels of concurrency (e.g., real-time applications
like chats
A brief history of Node.js(2018 - Present)
● Long-term Support (LTS): In 2018, the Node.js team
introduced the concept of LTS (Long-Term Support)
releases to ensure stability for production environments.
● Node.js is now maintained with clear LTS and current
release lines.
● Performance and Features: Node.js continues to evolve
with new features, including improvements in
performance, security, and better support for
asynchronous programming and debugging
A brief history of Node.js (Today)
● Global Impact: Node.js is widely used in both
startups and large corporations.
● Its performance, scalability, and vast
ecosystem of tools and libraries have made it
the go-to platform for many modern web and
mobile applications.
A brief history of Node.js (Today)
● Popular Usage:
○ It is used for back-end services, APIs,
microservices, and is integrated with
popular web frameworks such as
Express.js, NestJS, and Koa.js.
A brief history of Node.js (Today)
● Enterprise Adoption:
○ Many large companies, such as Netflix,
Uber, LinkedIn, Walmart, and PayPal, have
adopted Node.js for handling large-scale
applications and microservices.
Key Milestones in Node.js Development:
● 2009: Ryan Dahl creates and releases Node.js.
● 2010: Introduction of npm (Node Package Manager).
● 2015:Node.js Foundation established for governance.
● 2018: Introduction of LTS releases.
● 2019: Node.js merges with the JS Foundation to form
the OpenJS Foundation.
Advantages of Node.js
● Node.js is an open-source framework under
MIT license. (MIT license is a free software
license originating at the Massachusetts
Institute of Technology (MIT).)
● Uses JavaScript to build entire server side
application.
Advantages of Node.js
● Lightweight framework that includes bare
minimum modules. Other modules can be
included as per the need of an application.
● Asynchronous by default. So it performs
faster than other frameworks.
● Cross-platform framework that runs on
Windows, MAC or Linux
Traditional Web Server Model
● In the traditional web server model, each
request is handled by a dedicated thread
from the thread pool.
● If no thread is available in the thread pool at
any point of time then the request waits till
the next available thread.
Node.js Process Model
● Node.js processes user requests differently
when compared to a traditional web server
model.
● Node.js runs in a single process and the
application code runs in a single thread and
thereby needs less resources than other
platforms.
Node.js Process Model
● All the user requests to your web
application will be handled by a single
thread and all the I/O work or long running
job is performed asynchronously for a
particular request.
Node.js Process Model
● So, this single thread doesn't have to wait
for the request to complete and is free to
handle the next request.
● When asynchronous I/O work completes
then it processes the request further and
sends the response
Node.js Process Model
● An event loop is constantly watching for the
events to be raised for an asynchronous job
and executing callback function when the
job completes.
Advantages of Node.js
● Node.js process model increases the
performance and scalability with a few caveats.
● Node.js is not fit for an application which
performs CPU-intensive operations like image
processing or other heavy computation work
because it takes time to process a request and
thereby blocks the single thread.
Node.js Installation
● Node.js
● Node Package Manager (NPM)
● IDE (Integrated Development Environment)
or TextEditor
● NPM is included in Node.js installation since
Node version 0.6.0., so there is no need to
install it separately.
Node.js Installation
● Node.js comes with virtual environment
called REPL (aka Node shell). REPL stands
for Read-Eval-Print-Loop.
● It is a quick and easy way to test simple
Node.js/JavaScript code.
Node.js Module
● Module in Node.js is a simple or complex
functionality organized in single or multiple
JavaScript files which can be reused
throughout the Node.js application.
■ Core Modules(Built-in)
■ Local Modules
■ Third Party Modules
Node Modules
● Consider modules to be the same as JavaScript
libraries.
● A set of functions you want to include in your
application
Core Modules(Builts-in)
● The core modules include bare minimum
functionalities of Node.js.
● These core modules are compiled into its
binary distribution and load automatically
when Node.js process starts.
Core Modules
● http module includes classes, methods and events to create
Node.js http server.
● url module includes methods for URL resolution and parsing.
● Querystring module includes methods to deal with query
string.
● Path module includes methods to deal with file paths.
● Fs module includes classes, methods, and events to work with
file I/O.
● utilmodule includes utility functions useful for programmers.
Loading Core Modules
● In order to use Node.js core or NPM
modules, you first need to import it using
require() function as shown below.
● var module = require('module_name');
● The require() function will return an object,
function, property or any other JavaScript
type, depending on what the specified
module returns.
Node.js Local Module
● Local modules are modules created locally
in your Node.js application. These modules
include different functionalities of your
application in separate files and folders.
● You can also package it and distribute it via
NPM, so that Node.js community can use it.
Node.js Local Module(Your Modules)
● The module.exports is a special object
which is included in every JS file in the
Node.js application by default.
● Use module.exports or exports to expose
a function, object or variable as a module
in Node.js
Example module (date_time.js)
exports.date_time = function () {
return Date();
};
Use module
var http = require( 'http');

var dt = require( './date_time );

http.createServer( function (req, res) {

res.writeHead( 200, {'Content-Type' : 'text/html' });

res.write("The date and time are currently: " + dt.myDateTime());

res.end();

}).listen(8080);
END

You might also like