csci 3100 tutorial 3 javascript & node.js presented by su yuxin department of computer science...
TRANSCRIPT
1
CSCI 3100 Tutorial 3JavaScript amp Nodejs
Presented by SU Yuxin
Department of Computer Science and EngineeringThe Chinese University of Hong Kong
2
Outline
bull Introduction to JavaScriptbull JavaScript Basicbull Advanced Features of JavaScriptbull Introduction to Nodejsbull A Secrethellip
3
Do you know JavaScript
JavaJavaScrip
t
No Relationship
4
What is JavaScript
Script Language
bull Shellbull C
Dynamic
Type
bull Pythonbull Java
Objected-oriented
bull C++bull C
AsynchronousUnique Feature
5
Where to use JavaScript
bull The whole picturebrowser
CSS
HTML
HTML5
JavaScript Server Side
modifycreatedelete
data transmit
bull PHPbull Rails on Rubybull JavaScript
6
JavaScript Basic
bull Outputbull Variable amp Arithmeticbull Object amp Arraybull Functionbull IF amp Loop statement
7
JavaScript BasicOutput
bull No printf() bull Use consolelog() alert() instead
8
JavaScript BasicVariable amp Arithmetic
String can be in single or double quotes
Number can be integer or floating point
If there is no initial value the value is undefined
String concatenation
Integer Arithmetic
9
JavaScript BasicObject
You can use any property without declaration
You can also treat function as a property of the object and assign it like this
10
JavaScript BasicArray
Create an array
Initialization
Access an arrayUse it as a dictionary
Multi-type
Build-in functions and properties
11
JavaScript BasicFunction
Declaration
Argument amp Return value
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
2
Outline
bull Introduction to JavaScriptbull JavaScript Basicbull Advanced Features of JavaScriptbull Introduction to Nodejsbull A Secrethellip
3
Do you know JavaScript
JavaJavaScrip
t
No Relationship
4
What is JavaScript
Script Language
bull Shellbull C
Dynamic
Type
bull Pythonbull Java
Objected-oriented
bull C++bull C
AsynchronousUnique Feature
5
Where to use JavaScript
bull The whole picturebrowser
CSS
HTML
HTML5
JavaScript Server Side
modifycreatedelete
data transmit
bull PHPbull Rails on Rubybull JavaScript
6
JavaScript Basic
bull Outputbull Variable amp Arithmeticbull Object amp Arraybull Functionbull IF amp Loop statement
7
JavaScript BasicOutput
bull No printf() bull Use consolelog() alert() instead
8
JavaScript BasicVariable amp Arithmetic
String can be in single or double quotes
Number can be integer or floating point
If there is no initial value the value is undefined
String concatenation
Integer Arithmetic
9
JavaScript BasicObject
You can use any property without declaration
You can also treat function as a property of the object and assign it like this
10
JavaScript BasicArray
Create an array
Initialization
Access an arrayUse it as a dictionary
Multi-type
Build-in functions and properties
11
JavaScript BasicFunction
Declaration
Argument amp Return value
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
3
Do you know JavaScript
JavaJavaScrip
t
No Relationship
4
What is JavaScript
Script Language
bull Shellbull C
Dynamic
Type
bull Pythonbull Java
Objected-oriented
bull C++bull C
AsynchronousUnique Feature
5
Where to use JavaScript
bull The whole picturebrowser
CSS
HTML
HTML5
JavaScript Server Side
modifycreatedelete
data transmit
bull PHPbull Rails on Rubybull JavaScript
6
JavaScript Basic
bull Outputbull Variable amp Arithmeticbull Object amp Arraybull Functionbull IF amp Loop statement
7
JavaScript BasicOutput
bull No printf() bull Use consolelog() alert() instead
8
JavaScript BasicVariable amp Arithmetic
String can be in single or double quotes
Number can be integer or floating point
If there is no initial value the value is undefined
String concatenation
Integer Arithmetic
9
JavaScript BasicObject
You can use any property without declaration
You can also treat function as a property of the object and assign it like this
10
JavaScript BasicArray
Create an array
Initialization
Access an arrayUse it as a dictionary
Multi-type
Build-in functions and properties
11
JavaScript BasicFunction
Declaration
Argument amp Return value
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
4
What is JavaScript
Script Language
bull Shellbull C
Dynamic
Type
bull Pythonbull Java
Objected-oriented
bull C++bull C
AsynchronousUnique Feature
5
Where to use JavaScript
bull The whole picturebrowser
CSS
HTML
HTML5
JavaScript Server Side
modifycreatedelete
data transmit
bull PHPbull Rails on Rubybull JavaScript
6
JavaScript Basic
bull Outputbull Variable amp Arithmeticbull Object amp Arraybull Functionbull IF amp Loop statement
7
JavaScript BasicOutput
bull No printf() bull Use consolelog() alert() instead
8
JavaScript BasicVariable amp Arithmetic
String can be in single or double quotes
Number can be integer or floating point
If there is no initial value the value is undefined
String concatenation
Integer Arithmetic
9
JavaScript BasicObject
You can use any property without declaration
You can also treat function as a property of the object and assign it like this
10
JavaScript BasicArray
Create an array
Initialization
Access an arrayUse it as a dictionary
Multi-type
Build-in functions and properties
11
JavaScript BasicFunction
Declaration
Argument amp Return value
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
5
Where to use JavaScript
bull The whole picturebrowser
CSS
HTML
HTML5
JavaScript Server Side
modifycreatedelete
data transmit
bull PHPbull Rails on Rubybull JavaScript
6
JavaScript Basic
bull Outputbull Variable amp Arithmeticbull Object amp Arraybull Functionbull IF amp Loop statement
7
JavaScript BasicOutput
bull No printf() bull Use consolelog() alert() instead
8
JavaScript BasicVariable amp Arithmetic
String can be in single or double quotes
Number can be integer or floating point
If there is no initial value the value is undefined
String concatenation
Integer Arithmetic
9
JavaScript BasicObject
You can use any property without declaration
You can also treat function as a property of the object and assign it like this
10
JavaScript BasicArray
Create an array
Initialization
Access an arrayUse it as a dictionary
Multi-type
Build-in functions and properties
11
JavaScript BasicFunction
Declaration
Argument amp Return value
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
6
JavaScript Basic
bull Outputbull Variable amp Arithmeticbull Object amp Arraybull Functionbull IF amp Loop statement
7
JavaScript BasicOutput
bull No printf() bull Use consolelog() alert() instead
8
JavaScript BasicVariable amp Arithmetic
String can be in single or double quotes
Number can be integer or floating point
If there is no initial value the value is undefined
String concatenation
Integer Arithmetic
9
JavaScript BasicObject
You can use any property without declaration
You can also treat function as a property of the object and assign it like this
10
JavaScript BasicArray
Create an array
Initialization
Access an arrayUse it as a dictionary
Multi-type
Build-in functions and properties
11
JavaScript BasicFunction
Declaration
Argument amp Return value
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
7
JavaScript BasicOutput
bull No printf() bull Use consolelog() alert() instead
8
JavaScript BasicVariable amp Arithmetic
String can be in single or double quotes
Number can be integer or floating point
If there is no initial value the value is undefined
String concatenation
Integer Arithmetic
9
JavaScript BasicObject
You can use any property without declaration
You can also treat function as a property of the object and assign it like this
10
JavaScript BasicArray
Create an array
Initialization
Access an arrayUse it as a dictionary
Multi-type
Build-in functions and properties
11
JavaScript BasicFunction
Declaration
Argument amp Return value
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
8
JavaScript BasicVariable amp Arithmetic
String can be in single or double quotes
Number can be integer or floating point
If there is no initial value the value is undefined
String concatenation
Integer Arithmetic
9
JavaScript BasicObject
You can use any property without declaration
You can also treat function as a property of the object and assign it like this
10
JavaScript BasicArray
Create an array
Initialization
Access an arrayUse it as a dictionary
Multi-type
Build-in functions and properties
11
JavaScript BasicFunction
Declaration
Argument amp Return value
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
9
JavaScript BasicObject
You can use any property without declaration
You can also treat function as a property of the object and assign it like this
10
JavaScript BasicArray
Create an array
Initialization
Access an arrayUse it as a dictionary
Multi-type
Build-in functions and properties
11
JavaScript BasicFunction
Declaration
Argument amp Return value
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
10
JavaScript BasicArray
Create an array
Initialization
Access an arrayUse it as a dictionary
Multi-type
Build-in functions and properties
11
JavaScript BasicFunction
Declaration
Argument amp Return value
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
11
JavaScript BasicFunction
Declaration
Argument amp Return value
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
12
JavaScript BasicIF amp Loop statement
IF statement
FOR Loop
WHILE Loop
Note Anything defined will be treat as true Undefined will be treat as false
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
13
ADVANCED FEATURES
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
14
Regular Expression
bull First-class citizen in JavaScriptbull Create
var pattern = g$var pattern = new RegExp(ldquog$)
bull UseSoftware Engineeringsearch(pattern)textreplace(javascriptgi JavaScript)1 plus 2 equals 3match(d+g)123456789split()
Any expression ended with letter lsquogrsquo
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
15
Prototypebull function Range(from to)
thisfrom = from thisto = toRangeprototype = includes function(x) return thisfrom lt= x ampamp x lt= thisto foreach function(f) for(var x = Mathceil(thisfrom) x lt= thisto x++) f(x) toString function() return ( + thisfrom + + thisto + ) Here are example uses of a range objectvar r = new Range(13) Create a range objectrincludes(2) =gt true 2 is in the rangerforeach(consolelog) Prints 1 2 3consolelog(r) Prints (13)
It looks like the definition of class
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
16
Callback Functionbull function main(callback)
alert(I am main function) alert(Invoke callback function) callback()function b() alert(I am callback function b)function c() alert(I am callback function c)
function test() main(b) main(c)
bull Function is passed as a parameter
bull User-defined behavior at the end of a function or ldquoeventrdquo
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
17
Advanced Array Methods
bull Other similar functionsndash map()ndash filter()ndash every()ndash some()
Increase Each element by 1dataforEach(function(v i a) a[i] = v + 1)
var data = [12345]compute the sum of the array elementsvar sum = 0dataforEach(function(value) sum += value)consolelog(The sum is + sumtoString())
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
18
Asynchronous
Synchronously read a file Pass an encoding to get text instead of bytesvar text = fsreadFileSync(configjson utf8) Asynchronously read a binary file Pass a function to get the datafsreadFile(imagepng function(err buffer) if (err) throw err If anything went wrong process(buffer) File contents are in buffer)
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
19
INTRODUCTION TO NODEJS
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
20
Where to run my code
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
21
Where to run my code(conrsquod)
bull Chrome
bull Nodebull $ node testjs
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
22
IDE Jetbrains WebStorm
httpwwwjetbrainscomwebstorm You can register a student account with your cuhkeduhk email
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
23
What the Nodejs is designed for
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
24
Installation and HTTP setup
bull Install Nodejs from httpnodejsorgdownloadbull Use it to create a HTTP serverndash 1 In cmd of Windows type npm install http-server ndashg
bull If you have a HTTP proxy type this firstbull npm config set proxy httpproxycompanycomport
ndash 2 Start HTTP server http-server pathbull The path is the folder you want to share in webbull Your folder must have some files before it works ie indexhtml
(put lsquoit worksrsquo in it)
ndash 3 Open httplocalhost8080 in your browser
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
25
Modules
httpswwwnpmjscom
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
26
Event-based Design
var events = require(events)var emitter = new eventsEventEmitter()var username = colinvar password = password an event listeneremitteron(userAdded function(username password) consolelog(Added user + username)) add the user then emit an eventemitteremit(userAdded username password)
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
27
More details will coming at further tutorial
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-
28
Finally the secret ishellip
bull If you think JavaScript is powerfulhellipbull Butndash Too difficult to learnndash Too complex to writendash Too ugly to read
Try CoffeeScriptReference Google it
- CSCI 3100 Tutorial 3 JavaScript amp Nodejs
- Outline
- Do you know JavaScript
- What is JavaScript
- Where to use JavaScript
- JavaScript Basic
- JavaScript Basic Output
- JavaScript Basic Variable amp Arithmetic
- JavaScript Basic Object
- JavaScript Basic Array
- JavaScript Basic Function
- JavaScript Basic IF amp Loop statement
- Advanced features
- Regular Expression
- Prototype
- Callback Function
- Advanced Array Methods
- Asynchronous
- Introduction to Nodejs
- Where to run my code
- Where to run my code(conrsquod)
- IDE Jetbrains WebStorm
- What the Nodejs is designed for
- Installation and HTTP setup
- Modules
- Event-based Design
- More details will coming at further tutorial
- Finally the secret ishellip
-