Nodejs Course

Installing Node.js

To install Node.js, you can use an installer or a binary package, from node.js website: Node JS Downloads.
- I downloaded the windows binary package, then I extracted the files from the .zip archive into a directory named "nodejs", in partition" E" (you can install it in any directory you want).
Node.js Installation directory
- The official Node.js website has installation instructions on various platforms (Windows, Linux, Unix, Mac OS X, etc.): Installing Node.js via package manager.

Simple Node.js server script

Once you have downloaded and installed Node.js on your computer, you have to write a .js script that will create and start the server.
- In Node.js you use modules to build your script. A module is the same as a JavaScript library, that are included with the require() function.

1. I have Node.js (V.8) installed in "E:/nodejs/" directory. For tests, I created in this directory a folder named "test".
2. Now, in the "test" folder (E:/nodejs/test/) I create a file named "server.js" (you can choose any file name), and added the following code (Click on the code to select it):
//include the http module
const http = require('http');

//define constant for port
const port =8080;

//sets the server
const server = http.createServer((req, res)=> {
  res.writeHead(200, {'Content-Type':'text/plain'});
  res.write('Hello to me.');
  res.end();
});

//pass the port to server to listen to
server.listen(port, ()=> {
  console.log('Server running at //localhost:'+ port +'/');
});
- The code tells the computer to write "Hello to me." if anyone (e.g. a web browser) access the computer on port 8080.

3. The Node.js server must be initiated in the "Command Line Interface" program of your computer.
- To open the command line interface on windows, press the Start button and write "cmd" in the search field.

4. In the command line interface window I navigate to the folder that contains the Node.js application (E:/nodejs/), then, to initiate the "server.js" script, I write: "node test/server.js", and hit Enter.
In the command line interface window, node.js displays: "Server running at //localhost:8080/".
cmd start Node.js
- Now, the computer works as a server.

5. I open the browser, and I type in the address: http://localhost:8080
- Results:
browser Node.js
To simplify the starting of the node.js script, I created a Shorcut for cmd.exe that opens the command line interface directly in the "E:/nodejs" folder. To create this shortcut, follow these steps:
1. Right click on desktop and create New-Shorcut.
2. Browse to "windows/System32/" directory, and select "cmd.exe" file.
3. Once you have created the shortcut for cmd.exe, right click on it, and open Properties.
4. In the "Start in" field add the path to the directory where you installed Node.js.
cmd shortcut for Node.js folder
- When I click that shortcut, the command line interface opens directly in the "E:/nodejs"; then, i just write:
node script_name.js
• To close the Node.js server, press Ctrl+C in the command line interface window, or just close that cmd window.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Node.js Get Started

Last accessed pages

  1. JavaScript Course - Free lessons (31411)
  2. Properties and Methods of the Form elements (594)
  3. PHP Strings (3263)
  4. Selection Tools (8145)
  5. Get Lower, Higher, and Closest Number (5342)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (201)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (51)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide