Learning Node.JS Programming 

markdown=yes

April 2014  - --Better late than never, but I should have begun this process a few months ago.--

* <http://jothut.com/cgi-bin/junco.pl/tag/nodejs>
* <http://jothut.com/cgi-bin/junco.pl/b/17499/NodeJS-for-newbies>
* <http://nodejs.org>
* <https://www.npmjs.org>
* <http://nodejs.org/docs/v0.4.11/api/http.html#http.request>
* <http://package.json.nodejitsu.com>
* <http://inessential.com/2013/12/09/getting_started_with_node_js_for_cocoa_>
* <http://inessential.com/2013/12/09/getting_started_with_web_services_using_>
* <http://expressjs.com/4x/api.html>
* <http://howtonode.org/hello-node>
* <http://www.2ality.com/2011/12/nodejs-shell-scripting.html>
* <http://www.theprojectspot.com/tutorial-post/Node-js-for-beginners-part-1-hello-world/2>
* <http://www.theprojectspot.com/tutorial-post/nodejs-for-beginners-callbacks/4>

## Hello World at Command Prompt

Open up a text file and enter:

    // Call the console.log function.
    console.log("Hello World");

Save file as hello-console.js  and then at the command prompt, execute:  
`node hello-console.js`

Obviously, that produces:  
`Hello World`


## Hello World HTTP Server

Add the following code to a file called:  
`hello-server.js`

---

    // Load the http module to create an http server.
    var http = require('http');

    // Configure our HTTP server to respond with Hello World to all requests.
    var server = http.createServer(function (request, response) {
      response.writeHead(200, {"Content-Type": "text/plain"});
      response.end("Hello World\n");
    });

    // Listen on port 8080, IP defaults to 127.0.0.1
    server.listen(8080);

    // Put a friendly message on the terminal
    console.log("Server running at http://127.0.0.1:8080/");

---

Execute the code with:  
`node hello-server.js`

The following message will be displayed to the console:  
`Server running at http://127.0.0.1:8080/`

Point the browser to the domain/sub domain of the server with the port included. Example:  
<http://nodejs.soupmode.com:8080>

If running the server and the browser on the same machine, use:
`http://localhost:8080`

If the above server code is running, then the following will be displayed in the browser:  
`Hello World`

To stop this server app, hit `Ctrl-C` at the command prompt.


## Counter

Add the following code to `counter.js`

---

    var http = require('http');

    var userCount = 0;
    http.createServer(function (request, response) {
        console.log('New connection');
        userCount++;

        response.writeHead(200, {'Content-Type': 'text/plain'});
        response.write('Hello!\n');
        response.write('We have had '+userCount+' visits!\n');
        response.end();
    }).listen(8080);

    console.log('Server started');

---

Execute the code with:  
`node counter.js`

The message displayed to the console will say:  
`Server started`

When accessing the server at port 8080 in the browser, the following will be displayed to the browser:  
`Hello!`  
`We have had 1 visits!`

Back at the server, the console display will say:  
`New connection`  
`New connection`

A refresh on the browser will cause the following message to be sent by the counter.js app:  
`Hello!`  
`We have had 3 visits!`

The counter increments by two. 

Explanation from the [article](http://www.theprojectspot.com/tutorial-post/Node-js-for-beginners-part-1-hello-world/2) :

q.
Note: You might see the counter going up by two for each request, this is because your browser is requesting the favicon from the server (http://localhost:8080/favicon.ico).
br.
br. You should also see that Node.js is logging each request it receives to the console.
br.
br. The main thing we're doing here is setting a 'userCount' variable and incrementing on each request. We're then writing the 'userCount' in the response text.
q..

 #nodejs - #programming - #javascript - #howto