Node.js is an extremely light weight framework for rapidly developing and deploying next generation web and mobile apps. It enables developers to have full stack development. Not only does it save lines of code, but also saves a lot of time in writing those critical code.
Node.js is built on open source Chrome V8 engine. Its built on top of C++ layer. JS code is compiles into machine code for blazing execution on your machine or server.
This slide gives a jump start and a sneak peak for node.js.
About Parth:
Parth Joshi is a Tech - Entrepreneur and a Corporate Trainer. He has been part of two internet startups and has been lead technical architect and project manager. He has zeal for exploring new technology and how innovation solves problems of people at large. He currently acts as consultant for various startups. He also trains tech teams to make them startup ready. For more information about how Parth can train your team visit: www.parthjoshi.in/Training
Follow him on
Twitter: twitter.com/joshiparthin
Connect with him on LinkedIN : linkedin.com/in/joshiparthin
3. Evolution of Web Server and client
architecture
Client Server
HTTP Request
Response
(Static Documents)
Web Client (Browser):
Javascript
HTML
4. Evolution of Web Server and client
architecture
Client Server
Web Client (Browser):
Javascript
HTML
Web Servers:
Java
Python
PHP
Ruby
.NET
HTTP Request
Response
(Static Documents,
Dynamic Web Pages)
5. Evolution of Web Server and client
architecture
Client Server
Web Client (Browser):
Javascript
HTML
Mobile Client
Android
iOS
Native Clients
Java
Python
API Servers:
Java
Python
PHP
Ruby
.NET
HTTP Request
Response
Dynamic Pages
Static Documents
XML Response
6. Evolution of Web Server and client
architecture
Client Server
Web Client (Browser):
Javascript
HTML
Mobile Client
Android
iOS
Native Clients
Java
Python
API Servers:
Java
Python
Node
PHP
Ruby
.NET
HTTP Request
JSON Response
[Employee list]
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{ ...
8. Evolution of Web Server and client
architecture
How is JSON Used
Client Server
Web Client (Browser):
Javascript
HTML
Mobile Client
Android
iOS
Native Clients
Java
Python
API Servers:
Java
Python
Node
PHP
Ruby
.NET
HTTP Request
JSON Response
[Employee list]
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{ ...
9. Evolution of Web Server and client
architecture
The Idea behind Mongo DB
Client Server
Web Client (Browser):
Javascript
HTML
Mobile Client
Android
iOS
Native Clients
Java
Python
API Servers:
Java
Python
Node
PHP
Ruby
.NET
HTTP Request
JSON Response
[Employee list]
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{ ...
Store Data in form of JSON Objects
11. Chrome V8 Engine
• The V8 JavaScript Engine is an open source JavaScript engine
• developed by The Chromium Project for the Google Chrome web
browser
12. Chrome V8 Engine
Chrome V8 Engine
Javascript Code
Native Machine Code
• V8 compiles JavaScript to native machine code before
executing it.
• instead of more traditional techniques such as interpreting
bytecode or compiling the whole program to machine code
and executing it from a filesystem.
• The compiled code is additionally optimized (and re-optimized)
dynamically at runtime, based on heuristics of the code's
execution profile.
• Optimization techniques used include inlining, elision of
expensive runtime properties, and inline caching, among many
others.
Written in c/c++
13. So What’s Nodejs?
Node Js runs on Chrome V8 Engine
In Simple words:
“Node.Js is server side javascript framework”
14. So What’s Nodejs?
Node Js runs on Chrome V8 Engine
In not-so-simple Words :
“Node.js is a high-performance network applications framework,
well optimized for high concurrent environments.”
15. Highlights on node.js
• Node.js uses an event-driven, non-blocking I/O model, which makes
it lightweight.
• 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:
• var element = document.getElementById(“elementId”);
• Everything inside Node.js runs in a single-thread.
17. Lets get some hands on.
• Install node.js
• Install Visual Studio Code.
18. A Simple Hello world example
console.log ("Hello world with node");
Hello.js
Execute the code:
$ node Hello.js
Hello world with node
$ _
19. Let take some command line arguments
Use process object to access the command line arguments
Example:
process.argv[i] : to access the command line arguments
process.argv[i].length: to get the number of arguments
20. Print Command line arguments
var argument = "";
for (var i = 0; i < process.argv.length; i++) {
argument = process.argv[i];
console.log(argument);
}
File: comline.js
$node comline.js hello world
node
comline.js
hello
world
$node comline.js
node
comline.js
$node comline.js hello world
node
comline.js
hello
world
Execution:
21. Lets run a server now
// getting the http module
var http = require('http');
// creating server instance
var server = http.createServer(function(request, response){
response.writeHead(200, {'Content-Type' : 'text/html'});
response.write('Hello world<br/>');
response.end('bye bye');
});
// listening to server at port 9090
server.listen(9090,'127.0.0.1');
23. Understanding basics
Create a server instance
// creating server instance
var server = http.createServer(function(request, response){
response.writeHead(200, {'Content-Type' : 'text/html'});
response.write('Hello world <br/>');
response.end('bye bye');
});
As per the docs:
http.createServer([requestListener])
Returns a new instance of http.Server.
The requestListener is a function which is automatically added to the 'request' event.
26. Lets have a feel of non blocking IO
var fs = require('fs');
var data = fs.readFileSync("myText.txt");
var fs = require('fs');
fs.readFile('myText.txt',function(err,data){
…
});
Synchronous IO Async IO (Non Blocking)
27. File Streams
Input Streams
var fs = require('fs');
// creating a reading stream...
var readerStream = fs.createReadStream ("mytext.txt");
var dataRead = "";
readerStream.setEncoding('utf8');
28. File Streams
Events for Streams
data - This event is fired when there is data is available to read.
end - This event is fired when there is no more data to read.
error - This event is fired when there is any error receiving or writing data.
finish - This event is fired when all data has been flushed to underlying
system
29. File Streams
Input Streams
readerStream.on ('data', function (buffer){
dataRead += buffer;
}) ;
// when there is end in the stream
readerStream.on ('end', function (){
console.log(dataRead);
}) ;
// if there is error in the stream connection
readerStream.on ('error', function (err){
console.log("There is error:"+err);
}) ;
30. File Streams
var fs = require("fs");
var data = 'Simply Easy Learning';
// Create a writable stream
var writerStream = fs.createWriteStream('output.txt');
// Write the data to stream with encoding to be utf8
writerStream.write(data,'UTF8');
// Mark the end of file
writerStream.end();
Input Streams