node global scope •in a browser, global scope is the window object •in node.js,globalobject...
TRANSCRIPT
![Page 1: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/1.jpg)
Node.js#1
![Page 2: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/2.jpg)
What is Node.js?
• Node.js is an open-source server side runtime environment built on Chrome's V8 JavaScript engine.• It provides an event driven, non-blocking
(asynchronous) I/O and cross-platform runtime environment for building highly scalable server-side application using JavaScript.
![Page 3: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/3.jpg)
Types of applications by Node.js
• Command line application• Web application• Real-time chat application• REST API server
• Mainly used to build web servers, similar to PHP, Java, or ASP.NET
![Page 4: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/4.jpg)
Advantages of Node.js
• Open-source framework under MIT license. • Uses JavaScript to build entire server side application.• 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
![Page 5: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/5.jpg)
Traditional Web Server Model
![Page 6: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/6.jpg)
Node.js Process Model
![Page 7: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/7.jpg)
• node -v• npm -v •
![Page 8: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/8.jpg)
Node.js Basics
• Node.js includes following primitive types:• String• Number• Boolean• Undefined• Null• RegExp
• Everything else is an object in Node.js
![Page 9: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/9.jpg)
Node.js Basics
• Loose Typing• JavaScript in Node.js supports loose typing like the
browser's JavaScript• Use var keyword to declare a variable of any type
![Page 10: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/10.jpg)
Node.js Basics
• Object Literal
![Page 11: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/11.jpg)
Buffer
• Node.js includes an additional data type called Buffer (not available in browser's JavaScript)• mainly used to store binary data, while reading
from a file or receiving packets over the network.
![Page 12: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/12.jpg)
Defaults to local
• In the browser's JavaScript, variables declared without var keyword become global. • In Node.js, everything becomes local by default
![Page 13: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/13.jpg)
Access Global Scope
• In a browser, global scope is the window object• In Node.js, global object
represents the global scope.• To add something in global
scope, you need to export it using export or module.export• import modules/object using
require() function to access it from the global scope.
![Page 14: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/14.jpg)
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• some of the important core modules in Node.js
![Page 15: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/15.jpg)
Loading Core Modules
• using require() function
![Page 16: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/16.jpg)
Node.js Local Module
• Local modules are created locally in your Node.js application
![Page 17: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/17.jpg)
Loading Local Module
![Page 18: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/18.jpg)
Node Package Manager
• Node Package Manager (NPM) is a command line tool that installs, updates or uninstalls Node.js packages in your application• It is also an online repository for open-source
Node.js packages
• npm –v // check npm version• npm install npm –g //update it to the latest version
• Official website: https://www.npmjs.com
![Page 19: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/19.jpg)
Install Package Globally
• NPM can also install packages globally so that all the node.js application on that computer can import and use the installed packages• NPM installs global packages
into /<User>/local/lib/node_modules folder• Apply -g in the install command to install package
globally
![Page 20: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/20.jpg)
Uninstall Packages
• Update Package
• Uninstall Packages
![Page 21: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/21.jpg)
Node.js Web Server
• To access web pages of any web application, you need a web server• The web server will handle all the http requests for
the web application• Node.js provides capabilities to create your own
web server which will handle HTTP requests asynchronously• You can use IIS or Apache to run Node.js web
application but it is recommended to use Node.js web server
![Page 22: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/22.jpg)
Create Node.js Web Server
![Page 23: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/23.jpg)
Handle HTTP Request
![Page 24: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/24.jpg)
Handle HTTP Request
![Page 25: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/25.jpg)
Handle HTTP Request
• run the above web server
• To test
![Page 26: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/26.jpg)
Handle HTTP Request
![Page 27: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/27.jpg)
Sending JSON Response
![Page 28: node Global Scope •In a browser, global scope is the window object •In Node.js,globalobject represents the global scope. •To add something in global scope, you need to export](https://reader033.vdocument.in/reader033/viewer/2022052711/5ac23a587f8b9a4e7c8e0515/html5/thumbnails/28.jpg)
Read more.
• http://www.tutorialsteacher.com/nodejs/data-access-in-nodejs• https://www.w3schools.com/nodejs/nodejs_modules.asp• http://www.siamhtml.com/real-time-chat-with-node-js-and-socket-io/