Week 01 http://fog.ccsf.edu/~hyip Node.js Week 01 http://fog.ccsf.edu/~hyip.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

Java Script Session1 INTRODUCTION.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Node.js - What is Node.js? -
2440: 211 Interactive Web Programming Expressions & Operators.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Introduction to JavaScript Gordon Tian
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
An Introduction to Front-end Web Development Tom Perkins.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
Node.Js 1. 2 Contents About Node.Js Web requirement latest trends Introduction Simple web server creation in Node.Js Dynamic Web pages Dynamic web page.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
Node.JS introduction. What is Node.JS? v8 JavaScript runtime Event driven Non-blocking standard libraries Most APIs speak streams Provides a package manager.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
JavaScript and Ajax Week 10 Web site:
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introduction to Node.js® Jitendra Kumar Patel Saturday, January 31, 2015.
PHP using MySQL Database for Web Development (part II)
NodeJS and MEAN cs6320.
Chapter 4: Threads.
Node.Js Server Side Javascript
NodeJS and MEAN Prof. L. Grewe.
Representation, Syntax, Paradigms, Types
Variables and Arithmetic Operators in JavaScript
3 Things Everyone Knows About Node JS That You Don't
JavaScript Syntax and Semantics
Week 03 Node.js Week 03
Node.Js Server Side Javascript
Chapter 19 JavaScript.
Week 04 (Final Project) Node.js Week 04 (Final Project)
2017, Fall Pusan National University Ki-Joune Li
JavaScript and Ajax (Expression and Operators)
12 Asynchronous Programming
Starting JavaProgramming
During the last lecture we had a discussion on Data Types, Variables & Operators
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Programming in JavaScript
Representation, Syntax, Paradigms, Types
Multithreaded Programming
Units with – James tedder
Units with – James tedder
University of Kurdistan
Representation, Syntax, Paradigms, Types
INTRODUCTION TO By Stepan Vardanyan.
CS5220 Advanced Topics in Web Programming Node.js Basics
Programming in JavaScript
Focus of the Course Object-Oriented Software Development
CS5220 Advanced Topics in Web Programming More Node.js
Representation, Syntax, Paradigms, Types
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
Lecture 12: The Fetch Api and AJAx
Threads and Multithreading
The <script> Tag
Week 02 Node.js Week 02
Week 05 Node.js Week 05
Programming Languages and Paradigms
CS5220 Advanced Topics in Web Programming More Node.js
Web Programming and Design
Software Engineering and Architecture
Lecture 9: JavaScript Syntax
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

Week 01 http://fog.ccsf.edu/~hyip Node.js Week 01 http://fog.ccsf.edu/~hyip

What is Node.js? Node.js provides a non-blocking architecture using event loop processing based on single threaded JavaScript model and built on top of Google’s V8 JavaScript Engine. Event loop processing in Node.js is a programming style where the flow of execution is determined by events. Events are handled by event handlers or event callbacks. An event callback is a function that is invoked when something significant happens such as when the result of a database query is available. Therefore, Node.js is an event-driven or asynchronous programming. This means the current process will not block when it is doing I/O. Therefore, several I/O operations can occur in parallel, and each respective callback function will be invoked when the operation finishes. It’s important to know that event loop in Node.js is just one thread running inside one process, which means that, when an event happens, the event handler can run without interruption. In other words, there is at most one event handler running at any given time and any event handler will run to completion without being interrupted. Node’s core functionalities are kept to a minimum. There are many third-party modules supporting Node.js and can be downloaded, installed, and managed using Node Package Manager (NPM) to expand node’s functionalities. In a traditional server model, the web applications serving concurrent requests are implemented by using the thread pool model to handle incoming requests by assigning each incoming request to a separate thread of execution. In contrast, event model utilizes a single thread to process events, such as incoming requests from a queue.

Node.js background Node.js was invented in 2009 by Ryan Dahl along with other developers while working at Joyent. The Node.js’ Package Manager (NPM) was introduced in 2011, allowing publishing and sharing node.js source code by the community. Node.js was originally developed for the Linux environment. The first node.js to support Windows was released in July 2011. Node.js went through several technical changes including internal conflict over Joyent’s governance, resulting in Fedor Indutny (Node.js developer) starting io.js, a fork of Node.js. In February 2015, the Node.js foundation was announced, providing the opportunity of merging Node.js and io.js communities together under the Node.js foundation. On 10/12/2015, Node 4.2.0 (LTS) was announced, the first release covered under the new LTS plan (Long Term Support).

Features of Node.js Asynchronous and Event Driven: All APIs of Node.js library are asynchronous and non-blocking. Node.js server never waits for an API to return data. Server moves to next API after calling it and a notification mechanism of Events of Node.js helps server to get response from the previous API call. Single-Threaded and Highly Parallel: Other systems try to gain parallelism by running lots of code at the same time, typically by spawning many threads. But not Node.js, Node is a single-threaded environment. At most, only one line of your code will ever be executing at any time. Node gets away with this by doing most I/O tasks using non-blocking techniques. Rather than waiting line-by-line for an operation to finish, you create a callback that will be invoked when the operation eventually succeed or fails.

Overview of Blocking vs Non-Blocking Blocking is when the executing of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes. This happens because the event loop is unable to continue running JavaScript while a blocking operation is occurring. Synchronous methods in the Node.js are the most commonly used blocking operations. (Native modules may also have blocking methods) All of the I/O methods in the Node.js standard library provide asynchronous versions, which are non-blocking, and accept callback functions. Some methods also have blocking counterparts, which have names that end with sync.

Comparing Code Blocking methods execute synchronously and non-blocking methods executes asynchronously. Using the File System modules, this is a synchronous file read: const fs = require("fs"); const data = fs.readFileSync("/test.txt"); // blocks here until file is read Here is an equivalent asynchronous example: fs.readFile("/test.txt", function (err, data) { if(err) throw err; });

Blocking vs Non-Blocking codes Using the File System modules, this is a synchronous file read: const fs = require("fs"); const data = fs.readFileSync("/test.txt"); // blocks here until file is read console.log(data); moreWork(); // will run after console.log Here is an equivalent asynchronous example: fs.readFile("/test.txt", (err, data) => { if(err) throw err; }); moreWork(); // will run before console.log

Download and install Node.js Download and install latest version of Node.js Instruction for windows O/S click here (http://fog.ccsf.edu/~hyip/nodejs/week01/note/week_01_install_nodejs_win dows.doc). Instruction for Mac O/S click here (http://fog.ccsf.edu/~hyip/nodejs/week01/note/week_01_install_nodejs_ma c.doc). Create a working folder (nodejs_workspace). Check the Node.js version that just installed. c:\nodejs_workspace>node --version (or node –v)

Verify Installation Create a js file named hello_world.js in c:\nodejs_workspace with the following statement: console.log("Hello world!"); Now run the hello_world.js: C:\nodejs_workspace>node hello_world.js Verify the Output: Hello World!

JavaScript Language (review) Data types: 3 primitive data types (number, string, boolean). Composite Data Type: Object and Array. Special values: null, undefined, NaN, Infinity. Variables: first character can be letter or underscore, subsequent characters can be letter, number, or underscore. (No space, no reserved words). To define a variable: var name_of_var; Statement is ended with semicolon (;). var a = 3 + 4;

JavaScript Language (review) Expression will evaluate to a single value with single data type. var a = 3 + 4; // 3 + 4 is an expression, and will evulate to 7 (data type is number) Single line comment: // end with new line character \n Multiple line comment: /* start here second line */ Operators: Unary operator (one operand): -88, count++, !flag Binary operator (two operands): 7 + 8, num1 < num2 Ternary operator (three operands): (condition) ? True expression : False expression ;

JavaScript Language (review) 5 categories of operators: String operators: +, += Arithmetic operators: +, -, *, /, %, ++, -- Assignment operators: =, +=, -=, *=, /=, %= Comparison operators: ==, !==, >, >=, <, <=, (automatic data type conversion) === (equivalent), !=== (not equivalent) (No automatic data type conversion) Logical operators: &&, ||, ! Conditional Operator: var age = 16; (age < 18)? var answer="Minor" : var answer="Adult";

JavaScript Language (review) Special operators: delete (delete array entry or object) new (use to create new object) this (refer to current object) void (tell JavaScript to evaluate an expression and returns no value)

The order of operations () . [] new function() ! - + ++ -- void delete * / % + - < <= > >= == != === !== && || Conditional Operator = += -= *= /= %=

JavaScript Functions Function declaration: function function_name () { } Invoke function: function_name(); Passing parameters/arguments to function: function function_name(fname) { function_name("My Name"); Returning data/value from function: function function_name(a, b) { return a + b;

JavaScript selection If statement: if (condition) { // true statements } else { // false statements } If else if statement: if (condition) { // if condition is true } else if (cond2) { // if cond2 is true } else { // catch all statement

JavaScript switch statement switch (expression) { case label1: // statements break; case label2: default: //statements; }

JavaScript Loops JavaScript for loop: for (var i=0; i < 3; i++) { // statements; } JavaScript while loop: var i = 0; while (i < 3) { i++;

JavaScript do while loop var i = 0; do { // statements; i++; } while(i < 3); NOTE: special statements: break; // get out of inner loop continue; // go to testing and repeat the loop

JavaScript Arrays Create JavaScript Array: var js_ary = ["John", "Doe"]; OR var js_ary = new Array(); js_ary[0] = "John"; js_ary[1] = "Doe";

JavaScript Objects Dot notation: object_name.property_name object_name.method_name(parameter, argument); Create JavaScript Object: var js_obj = { firstName : "John", lastName : "Doe" }; OR var js_obj = new Object(); js_obj.firstName = "John"; js_obj.lastName = "Doe“;

References Node.js the Right Way, Jim R. Wilson, The Programatic Bookshelf, ISBN 978-1-937785-73-4 NodeJS: Practical Guide for Beginners, Matthew Gimson, ISBN 978-1- 519354-07-5 Express.js: Node.js Framework for Web Application Development, Daniel Green, ISBN 978-1-530204-06-9 Express.com Tutorials Point The Node Beginner Book, Manuel Kiessling, Leanpub, Link.