Top Banner
Node.js #1
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

Apr 02, 2018

Download

Documents

tranhuong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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

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

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

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

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

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

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

• 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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/