nodejs guide for beginners
TRANSCRIPT
BYC.ENOCH JOSHUA
What’s I/O?Distance of data travel from input to output.
What is JavaScript ? It is dynamic programming language.
It is client side script to interact with user.
It most commonly used as a part of web browser
Background Node.js was invented in 2009 by Ryan Dahl inspired to create Node.js after seeing a file upload progress
bar on FLICKR Open source , sponsored by Joyent and Microsoft Ryan was a mathematics student and dropped out of
college. Node.js runs on V8.
Introduction: Basic
What is NodeJS ?NodeJS is an open source , cross platform runtime environment for server side and networking application. It is written in JavaScript and can run on Linux , Mac ,
Windows , FreeBSD. It provided an event driven architecture and a non blocking I/O
that optimize and scalability. These technology uses for real time application.
It used Google JavaScript V8 Engine to Execute Code.
JavaScript V8 EngineThe V8 JavaScript Engine is an open source JavaScript engine developed by Google for the Google Chrome web browser.V8 compiles JavaScript to native machine code (IA-32, x86-64, ARM, or MIPS ISAs) before executing it.V8 can run standalone, or can be embedded into anyC++ application
Why Use NodeJS? Node's goal is to provide an easy way to build scalable
network programs.
What is unique about NodeJS? Using JavaScript on server-side making communication
between client and server will happen in same language. Servers are Event based serves each request in Evented
Loop. So, it can handle simultaneous requests.
Reasons to use NodeJS It is very lightweight and fast. There has been over 200,000
visits on this website in three weeks and minimal server resources has been able to handle it all.
The counter is really easy to make to be real time. Node.js was easy to configure. There are lots of modules available for free. NodeJS work with NoSQL as well.
When not use NodeJS Your server request is dependent on heavy CPU consuming
algorithm/Job.
NodeJS SetupDownload at http://nodejs.orgSet Environment variable (Last version automatic set)
Open command line and type following.
node -v
What is Callback? Call-back is an asynchronous equivalent for a function. A call-back function is called at the completion of a given
task. Node makes heavy use of call-backs.
Blocking I/O: Traditional applications waits for a set code to complete before
it displays the content. This is called blocking code.
Non blocking I/O: In non blocking Once the request is made we continue on to
the next line of code before waiting for the time consuming request to finish.
Uses callback functions to handle non blocking code.
What makes node better?
Event driven
• Just like java script events on the client, Node can handle various events on the server
• Examples of server events are file transfer , request for a page , loading content from database
Node.js Event Loop
REPL TerminalRead − Reads user's input, parses the input into JavaScript data-
structure, and stores in memory.Eval − Takes and evaluates the data structure.Print − Prints the result.Loop − Loops the above command until the user presses ctrl-
c twice.
Starting REPL
Simple Expression
node >
node > 1 + 3 4>1 + ( 2 * 3 ) - 4 3>
REPL Commandsctrl + c − terminate the current command.ctrl + c twice − terminate the Node REPL.ctrl + d − terminate the Node REPL.Up/Down Keys − see command history and modify previous
commands.tab Keys − list of current commands..help − list of all commands..break − exit from multiline expression..clear − exit from multiline expression..save filename − save the current Node REPL session to a file..load filename − load file content in current Node REPL
session.
What is npm?
NPM
Easy for developers to share and reuse code
Makes it easy to update the code that you’re sharing
NPM Functionalities
Functionalities
Online repositories
Command line utility
To install Version management
Dependency management
The npm comes bundled with Node.js installables in versions after that v0.6.3. You can check the version by command.
npm –v (or) npm version
Installing Modules using npm
Following is the syntax to install any Node.js module:
For example:npm install <Module Name>
npm install express
Local Installation
Global Installation
• Local Mode
Package installation
• Node module
accessible• require(
)
command
• Npm ls
• Global Mode
Package installation
• System Directory
Not-accessible • Require(
)
command
• Npm ls -g
CLI
Uninstalling a Modulenpm uninstall express
Updating a Modulenpm update express
Search a Modulenpm search express
BUFFER For an input operation, buffer mode will wait until all the data
is collected into a buffer and then pass it to a call-back as soon as the entire resource
is read. In the below image you can see that when some data are
received it is saved into the buffer. But when everything is received i.e. the final one which
causes the entire buffer to be sent to the customer.
STREAMS Whereas stream will allow you to process the data as soon as
it arrives from the resource. In the below image each new chunk of data is received from
the resource is immediately provided to the consumer, thus consumer can process the data straightaway without
waiting for all the data to be collected in the buffer.
Stream Types
Types of Streams
used for read operation.
Readable
used for write operation.
Writeable
used for both read and write
operation.
Duplex
output is computed based on
input.Transform
File System Node implements File I/O using simple wrappers around
standard POSIX functions. The Node File System (fs) module can be imported using the
following syntax File system is of 2 types (i) Synchronous (ii) Asynchronous
Synchronous: synchronous method never blocks a program during its
execution, whereas the second one does.
var fs = require("fs")
Asynchronous Asynchronous methods take the last parameter as the
completion function call-back and the first parameter of the call-back function as error.
It is better to use an asynchronous method instead of a synchronous method
Scalable
Node applications are highly scalable
Horizontal scaling of servers
Adding more servers instead of increasing the performance of each
What is a Web Server?
A Web Server is a software application which handles HTTP requests
sent by the HTTP client, like web browsers, and returns web pages in response to the clients.
Web servers usually deliver html documents along with images, style sheets, and scripts.
Most of the web servers support server-side scripts
Web Application Architecture
Client − This layer consists of web browsers, mobile browsers or applications which can make HTTP requests to the web server.
Server − This layer has the Web server which can intercept the requests made by the clients and pass them the response.
Business − This layer contains the application server which is utilized by the web server to do the required processing. This layer interacts with the data layer via the database or some external programs.
Data − This layer contains the databases or any other source of data.
Who uses NodeJS
WHAT IS
Full-stack JavaScript solution that helps you build fast, robust and maintainable production web applications using:
WHAT DOES IT M.E.A.N.?
express
WHY M.E.A.N.?SAME LANGUAGE, SAME OBJECTS.
{“_id”: ObjectId(“512638a28b799”), “username” : “symonny” }
{“_id”: “512638a28b799”,“username” : “symonny” }
{“_id”: “512638a28b799”,“username” : “symonny” }
❖ NoSqL cross-platform document-orienteddatabase system
❖ JSON-like documents with dynamic schemas
❖ Easier & faster to send data between
❖ client and server (b/c data saved in JSON format)
❖ Local Install:
➡ MongoDB: http://www.mongodb.org/
❖ MongoDB-as-a-Service:
➡ Modulus: https://modulus.io
➡ Mongolab: https://mongolab.com
❖ A lightweight framework used to build single and multi-page web applications in Node.JS
❖ Wrapper for the core Node.js HTTP module objects.
❖ Provides functions for everythingyou may need to build a modern web server
❖ Learn More: http://expressjs.com
express
❖ Client-side MVC framework: http://angularjs.org❖ Problem: Updating page without reload
Angular.js declarative, 2-way data❖ Solution: binding
JavaScript platform built on V8❖ Server-side engine
❖ Helps building highly scalable and concurrent applications rapidly
❖ Makes multithreaded server easy
❖ Event based concurrency
❖ Easy to modify and maintains apps, due to piped modules
❖ Install from http://nodejs.org
APPS SUITED FOR NODE.JS
❖ E-Commerce
❖ Payment Processing
❖ Social Media
❖ Realtime Services
❖ Media Applications
❖ Enterprise Services