Node.js

 
 
Node.js is a server-side software system designed for writing scalable Internet applications, notably web servers.
It is build on built on Chrome's JavaScript runtime. Programs are written on the server side in JavaScript, using event-driven, asynchronous I/O to minimize overhead and maximize scalability.

Node.js contains a built-in HTTP server library, making it possible to run a web server without the use of external software, such as Apache or Lighttpd, and allowing more control of how the web server works. Node.js enables web developers to create an entire web application in JavaScript, both server-side and client-side.

The latest Node.js version can be downloaded from: http://nodejs.org

Even numbered versions (0.4, 0.6, 0.8) are stable, and odd numbered versions (0.3, 0.5) are unstable. The stable releases are API-stable, which means that if you are using 0.8.1 and 0.8.2 comes out, you should be able to upgrade with no issues.







Node.js examples



Information
none

Operating system used
Windows Vista Home Premium SP 2

Software prerequisites
Node.js 0.10.11


No Description Files
1 Read a text file asynchronously Step 1:
Right click and download the files in project folder "example1":
Step 2:
Run this example, type:
node read_txt_file

2 Read a text file synchronously Step 1:
Right click and download the files in project folder "example2":
Step 2:
Run this example, type:
node read_txt_file_sync

3 Read a json file synchronously Step 1:
Right click and download the files in project folder "example3":
Step 2:
Run this example, type:
node read_json_file_sync

4 Write to text file synchronously Step 1:
Right click and download the file in project folder "example4":
Step 2:
Run this example, type:
node write_txt_file_sync

5 Write to text file asynchronously Step 1:
Right click and download the file in project folder "example5":
Step 2:
Run this example, type:
node write_txt_file

6 Read json file synchronously and watch if json file is changed
Note: To stop node press CTRL+C
Step 1:
Right click and download the files in project folder "example6":
Step 2:
Run this example, type:
node read_json_file_sync

7 Simple webserver listening to port 9080
Note: To stop the webserver press CTRL+C
Step 1:
Right click and download the file in project folder "example7":
Step 2:
Start webserver, type:
node webserver

Step 3:
Open web browser, type:
http://localhost:9080/

8 Simple webserver listening to port 9080 and showing web pages within directory demo
Note: To stop the webserver press CTRL+C
Step 1:
Right click and download the file in project folder "example8":
Step 2:
Right click and download the files in project folder "example8/demo":
Step 3:
Start webserver, type:
node webserver2

Step 4:
Open web browser, type:
http://localhost:9080/demo/page1.html
http://localhost:9080/demo/page2.html

8b Simple webserver listening to port 9080 and showing web pages within directory demo using the connect module.

Note: To stop the webserver press CTRL+C
Step 1:
Right click and download the files in project folder "example8b":
Step 2:
Right click and download the files in project folder "example8b/demo":
Step 3:
Install connect as local module, type:
npm install

Step 4:
Start webserver, type:
node webserver8b

Step 5:
Open web browser, type:
http://localhost:9080/page1.html
http://localhost:9080/page2.html
http://localhost:9080/

8c Simple webserver listening to port 9080 and showing a web form within directory demo using the connect module.
After submitting the web form, the submitted data is shown on a second page.

Note: To stop the webserver press CTRL+C
Step 1:
Right click and download the files in project folder "example8c":
Step 2:
Right click and download the file in project folder "example8c/demo":
Step 3:
Install connect as local module, type:
npm install

Step 4:
Start webserver, type:
node webserver8c

Step 5:
Open web browser, type:
http://localhost:9080/form1.html

Step 6:
Enter your name and age and submit the form:

You should see:

Your name is: your_name
Your age is: your_age


8d Simple webserver listening to port 9080 and showing the correct response code and content type using the connect module.


Note: To stop the webserver press CTRL+C
Step 1:
Right click and download the files in project folder "example8d":
Step 2:
Install connect as local module, type:
npm install

Step 3:
Start webserver, type:
node webserver8d

Step 4:
Open web browser, type:
http://localhost:9080/showdata

You should see:

{"name":"Lena Heady","born":"3 October 1973","occupation":"acctress"}

Step 5:
Open web browser, type:
http://localhost:9080

You should see:

Forbidden.

9 Simple webserver read json config file to get ip address and portnumber. Show web pages within directory demo
Note: To stop the webserver press CTRL+C
Step 1:
Right click and download the files in project folder "example9":
Step 2:
Right click and download the files in project folder "example9/demo":
Step 3:
Start webserver, type:
node webserver3

Step 4:
Open web browser, type:
http://localhost:9080/demo/page1.html
http://localhost:9080/demo/page2.html


10 Simple webserver read json config file to get ip address and portnumber. Show web pages within directory demo.
The json config file is watched in case it is updated.

Note: To stop the webserver press CTRL+C
Step 1:
Right click and download the files in project folder "example10":
Step 2:
Right click and download the files in project folder "example10/demo":
Step 3:
Start webserver, type:
node webserver4

Step 4:
Open web browser, type:
http://localhost:9080/demo/page1.html
http://localhost:9080/demo/page2.html

11 Simple webserver using express module.
Read json config file to get ip address and portnumber.
Show web pages within directory demo.

Note: To stop the webserver press CTRL+C
Step 1:
Right click and download the files in project folder "example11":
Step 2:
Install express in your project, type:
npm install express

Step 3:
Right click and download the files in project folder "example11/demo":
Step 4:
Start webserver, type:
node webserver5

Step 5:
Open web browser, type:
http://localhost:9080
http://localhost:9080/page1.html
http://localhost:9080/page2.html

12 Simple webserver using express module.
Read json config file to get ip address and portnumber.
Show web pages within directory demo.
Show text in URL path.
Read ID number in URL path and match ID number with json message.

Note: To stop the webserver press CTRL+C
Step 1:
Right click and download the files in project folder "example12":
Step 2:
Install express in your project, type:
npm install express

Step 3:
Right click and download the files in project folder "example12/demo":
Step 4:
Start webserver, type:
node webserver6

Step 5:
Open web browser, type:
http://localhost:9080
http://localhost:9080/page1.html
http://localhost:9080/page2.html
http://localhost:9080/hello/robert
http://localhost:9080/user/1
http://localhost:9080/user/2

13 Show all repositories for a specific user on GitHub Step 1:
Right click and download the file in project folder "example13":
Step 2:
Run this example, type:
node github

14 Make several methods public in the module module1.js Step 1:
Right click and download the files in project folder "example14":
Step 2:
Run this example, type:
node server1

15 Make the method getRepos public in the module github2.js.
Show all repositories for a specific user on GitHub.
Step 1:
Right click and download the files in project folder "example15":
Step 2:
Run this example, type:
node server2

16 Show text in different colors Step 1:
Right click and download the file in project folder "example16":
Step 2:
Install colors in your project, type:
npm install colors

Step 2:
Run this example, type:
node text_colors

17 Create a database and collection in MongoDB.
Store and and search objects in the collection.
Step 1:
Install mongoDB and start the mongoDB server

If mongoDB server is not automatically started, goto <mongoDB_installation_directory>/bin and run mongod.exe

Step 2:
Right click and download the files in project folder "example17":
Step 3:
Install mongodb module in your project, type:
npm install mongodb

Step 4:
Create mongo database "nodejs-demo" and store objects in the "user" collection, type:
node connect_db

Step 5:
Search objects in the "user" collection, type:
node query_db

18 Create a database and collection in MongoDB.
Store objects in the collection.
Search objects in the collection using your browser.

Note: To stop the webserver press CTRL+C
Step 1:
Install mongoDB and start the mongoDB server

If mongoDB server is not automatically started, goto <mongoDB_installation_directory>/bin and run mongod.exe

Step 2:
Right click and download the files in project folder "example18":
Step 3:
Install express in your project, type:
npm install express

Step 4:
Install mongodb module in your project, type:
npm install mongodb

Step 5:
Right click and download the files in project folder "example18/demo":
Step 6:
Create mongo database "nodejs-demo" and store objects in the "user" collection, type:
node connect_db

Step 7:
Start webserver, type:
node webserver7

Step 8:
Search objects in the "user" collection using the browser, type:
http://localhost:9080/user/1
http://localhost:9080/user/2
http://localhost:9080/user/3

19 Looking at the Twitter streaming API.
  • Connect to the Twitter streaming API using oAuth
  • Collect tweets around a topic in real time
Note: To stop the script press CTRL+C
Step 1:
Create a twitter account if you do not have one: https://twitter.com/signup

Step 2:
Create an application at https://dev.twitter.com/ and get the following oAuth settings:
  • Consumer key
  • Consumer secret
  • Access token
  • Access token secret
Step 3:
Right click and download the file in project folder "example19":
Step 4:
Install ntwitter in your project, type:
npm install ntwitter

Step 5:
Start script, type:
node twitter

20 Looking at the Twitter streaming API and store tweets in mongoDB
  • Connect to the Twitter streaming API using oAuth
  • Collect tweets around a topic in real time
  • Store the tweets in mongoDB
Note: To stop the script press CTRL+C
Step 1:
Install mongoDB and start the mongoDB server

If mongoDB server is not automatically started, goto <mongoDB_installation_directory>/bin and run mongod.exe

Step 2:
Create a twitter account if you do not have one: https://twitter.com/signup

Step 3:
Create an application at https://dev.twitter.com/ and get the following oAuth settings:
  • Consumer key
  • Consumer secret
  • Access token
  • Access token secret
Step 4:
Right click and download the file in project folder "example20":
Step 5:
Install ntwitter in your project, type:
npm install ntwitter

Step 6:
Install mongodb module in your project, type:
npm install mongodb

Step 7:
Start script, type:
node twitter2

21 Looking at the Twitter streaming API and store tweets in mongoDB. Open a browser and show last 10 tweets from mongoDB.

Reload page to show updated tweets.
  • Setup webserver
  • Connect to the Twitter streaming API using oAuth
  • Collect tweets around a topic in real time
  • Store the tweets in mongoDB
  • Open a browser and show last 10 tweets from mongoDB
Note: To stop the script press CTRL+C
Step 1:
Right click and download the files in project folder "example21":
Step 2:
Install mongoDB and start the mongoDB server

If mongoDB server is not automatically started, goto <mongoDB_installation_directory>/bin and run mongod.exe

Step 3:
Create a twitter account if you do not have one: https://twitter.com/signup

Step 4:
Create an application at https://dev.twitter.com/ and get the following oAuth settings:
  • Consumer key
  • Consumer secret
  • Access token
  • Access token secret
Step 5:
Install ntwitter in your project, type:
npm install ntwitter

Step 6:
Install mongodb module in your project, type:
npm install mongodb

Step 7:
Install express module in your project, type:
npm install express

Step 8:
Start webserver, type:
node webserver8

Step 9:
Show last 10 tweets in browser, type:
http://localhost:9080

23 Looking at the Twitter streaming API. Send the data in realtime to a browser using module socket.io.
No database used.
  • Setup webserver
  • Connect to the Twitter streaming API using oAuth
  • Collect tweets around a topic in real time
  • Send the tweets in realtime to a browser using module socket.io
Note: To stop the script press CTRL+C
Step 1:
Right click and download the files in project folder "example23":
Step 2:
Create a twitter account if you do not have one: https://twitter.com/signup

Step 3:
Create an application at https://dev.twitter.com/ and get the following oAuth settings:
  • Consumer key
  • Consumer secret
  • Access token
  • Access token secret
Step 4:
Install ntwitter in your project, type:
npm install ntwitter

Step 5:
Install express module in your project, type:
npm install express

Step 6:
Install socket.io module in your project, type:
npm install socket.io

Step 7:
Start webserver, type:
node webserver10

Step 8:
Show tweets in browser, type:
http://localhost:9080

24 Use of the os (operating system) module Step 1:
Right click and download the file in project folder "example24":
Step 2:
Run this example, type:
node script1

You could see:
Node.js version=v0.10.32, Operating system=Windows_NT

Note:
The os module is part of the core Node.js module. You do not need to install the os module separately.

25 Use the REPL (Read Evaluate Print Loop) interface Step 1:
Open a terminal and type:
node

Step 2:
A prompt ">"is shown, the REPL interface is ready to enter javascript commands.

Step 3:
You can enter:
  • os=require("os")

    This will show all available functions provided by the os module.

  • os.type()

    To execute the os.type() function.

  • global

    To show all variables available in the global scope.

  • CTRL+D or
    CTRL+C, CTRL+C or
    process.exit()

    To stop the REPL interface.
26 Use of the node-dev module.

This module automatically shows changes to a file without having to stop and start node.
Step 1:
Right click and download the file in project folder "example26":
Step 2:
Install node-dev as a global module, type:
npm install node-dev -g

Step 3:
Run this example, type:
node-dev webserver

Step 4:
Open web browser, type:
http://localhost:9080/

Step 5:
Change the "Hello world" response in webserver.js, for example "Hello galaxy".
When you save this file, the changes will be shown in the browser without having to stop and start the node.

27 There are several types of modules:
  • Core modules

    There modules are shipped with Node.js and do not need to be separately downloaded and installed, for example the os module.
    See example 24.

  • Modules that are part of your project

    These modules are installed using npm and are stored in the "node-modules" folder within your project folder, for example the colors module.
    See example 16.

  • Local modules to separate data from your code

    See example on right.

Step 1:
Right click and download the files in project folder "example27":
Step 2:
Run this example, type:
node script2

Script2.js loads data from data1.js file, by using statement require("./data1.js")

Step 3:
You should see:
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

28 Read command line arguments using the optimist module. Step 1:
Right click and download the file in project folder "example28":
Step 2:
Install optimist as local module, type:
npm install

Step 3:
Run this example, type:
node script3 --myflag 5 --myvalue 100 hello world

You should see:
Number 1. Value=100. Unused argument:hello
Number 2. Value=100. Unused argument:hello
Number 3. Value=100. Unused argument:hello
Number 4. Value=100. Unused argument:hello
Number 5. Value=100. Unused argument:hello


Note 1:
The first unused argument "hello" is stored in _array[0], the second unused argument "world" is stored in _array[1].

Note 2:
Changing the order of the flags does not matter, you can also type:
node script3 hello --myvalue 100 world --myflag 5

29 Enter input from command line using the readline module. Step 1:
Right click and download the file in project folder "example29":
Step 2:
Run this example, type:
node script4

You should see:
How old are you? Enter a number:

Enter a number to see the response.

30 Use module jshint to check if your javascript code contains errors before you actually run it. Step 1:
Right click and download the files in project folder "example30":
Step 2:
Install jshint globally, type:
npm install jshint -g

Step 3:
Run this example, type:
jshint script5_ok.js

You should see:
No messages

Step 4:
Run this example, type:
jshint script5_nok.js

You should see:
Many warning messages