0% found this document useful (0 votes)
14 views

Introduction To Node - Js

Uploaded by

Jomei Custorio
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Introduction To Node - Js

Uploaded by

Jomei Custorio
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to Node.

js
About Node.js
• Created by Ryan Dahl in 2009

• MIT License

• Based on Google Chrome JavaScript V8 Engine


What is Node.js?
Node.js is a platform built on chrome’s Javascript runtime
for easily building, fast and scalable network application.
Node.js uses an event-driven, non-blocking l/O model
that make it lightweight and efficient, perfect for data-
intensive real-time application that run accros distributed
devices.
Other projects like Node.js
● Vert.x => Polygot programming

● Akka => Scala and Java

● Tornado => Python

● Libevent => C

● EventMachine => Ruby


Why node.js ?
● Non Blocking I/O
● V8 Javascript Engine
● Single Thread with Event Loop
● Windows, Linux, Mac
● 1 Language for Frontend and Backend
The idea behind node.js
• Perform asynchronous processing on single thread instead of classical multithread processing,
minimize overhead & latency, maximize scalability

• Scale horizontally instead of vertically

• Ideal for applications that serve a lot of requests but dont use/need lots of computational power
per request

• Not so ideal for heavy calculations, e.g. massive parallel computing

• Also: Less problems with concurrency


Node.js Event Loop
There are a couple of implications of this apparently very simple and basic model

• Avoid synchronous code at all costs because it blocks the event loop

• Which means: callbacks, callbacks, and more


callbacks
Blocking vs Non-Blocking
Example :: Read data from file and show data
Blocking
● Read data from file
● Show data
● Do other tasks
var data = fs.readFileSync( “test.txt” );
console.log( data );
console.log( “Do other tasks” );
Non-Blocking Callback

● Read data from file


When read data completed, show data
● Do other tasks

fs.readFile( “test.txt”, function( err, data ) {


console.log(data);
Node.js for….

● Web application
● Websocket server
● Ad server
● Proxy server
● Streaming server
● Fast file upload client
● Any Real-time data apps
● Anything with high I/O
Application of node.js
• NodeJS should be preferred to build Real-Time
Chats, Complex Single-Page applications, Real-
time collaboration tools, Streaming apps, JSON
APIs based application.
• There are some frameworks of the node which you
can use to build your applications. Some popular
frameworks of node are…Express.js, Socket.io,
Koa.js, Meteor.js, Sail.js.
The Built-in HTTP module
Node.js has a built-in module called HTTP,
which allows Node.js to transfer data over the
Hyper Text Transfer Protocol (HTTP).
To include the HTTP module, use the require()
method:

var http = require('http');


Node.js as a Web Server
The HTTP module can create an HTTP server that
listens to server ports and gives a response back to
the client.

Use the createServer() method to create an HTTP


server:

The function passed into the


http.createServer() method, will be
executed when someone tries to access the
computer on port 8080.
Save the code above in a file called
"demo_http.js", and initiate the file:
Initiate demo_http.js:
C:\Users\Your Name>node demo_http.js
Advantages of node.js
• Easy Scalability
• Real-time web apps
• Fast suite
• Easy to learn code
• Advantageod caching
• Data Streaming
• Hosting
• Corporate support
Concept of node.js
The following diagram depicts some important parts of Node.js that are useful and help us understand it
better.
•MODULES - are like javascript libraries that can be used in node.js
application to include a set of functions.

To include a module in a node.js application use the require() function with the
parenthesis containing the name of the module.

•CONSOLE- is a module that provides a way to debug similar to that of the


JavaScript console provided by the internet browser.

•CLUSTER - is a module that allows multi threading by vreating child


prosesses that share the same server port and run simultaneously.
• GLOBAL- global objects in node.js is available in all modules.These
objects are functions, module, strings etc.

Some node.js global objects


• ERROR HANDLING- errors in node.js are handled
through exceptions.

• STREAMS - streams are objects that let you write data or


read data continuously.

There are four types of streams.


-Readable
-Writable
-Duplex
-Transform
• BUFFER - is a module that allowshandling streams that
contain only binary data.

• DOMAIN - module intercepts errors that remain unhandled.

Two methods for intercepting.


-internal binding
-external binding
• DNS - DNS module is used to connect to dns server
and perform name resolution.

DNS module can also be used for performing name


resolution without a network communication by using
the method dns.resolve()

•DEBUGGER - Debugger can be used in the terminal


by using the "inspect" keyword before the name of
JavaScript file.

You might also like