«

»

Feb 25

NodeJS Hello World

NodeJS hello world is pretty straight forward

  1. Download NodeJS & install it
    • Verify via running node on command line
  2. Type up the following hello.js in your favorite editor
    var http = require('http');
    
    http.createServer(function (request, response) {
      response.writeHead(200, {'Content-Type': 'text/plain'});
      response.end('Hello World\n');
    }).listen(8080);
    
    console.log('Server running at http://127.0.0.1:8080/');
  3. Run
    node hello.js
  4. You’ll see Hello World printed when you visit http://localhost:8080

Counting Hello World

A counting hello world is also pretty straight forward, as Javascript has closure, so we just need to setup a variable to store the counter.

  1. Change hello.jsand add a count variable as follows
    var http = require('http');
    var count = 0;
    http.createServer(function (req, res) {
                res.writeHead(200, {'Content-Type': 'text/plain'});
                count += 1;
                res.end('Hello World ' + count + '\n');
        }).listen(8080, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:8080/');
  2. Refresh the page and you’ll see the counter being updated.
  3. You might find the counter being updated in increments of 2 – this is due to your browser issue an “invisible request” (because we are not logging it) for the non-existent /favicon.ico file as well as for the root. To see it in action, we’ll add a logging line to see the request url via req.url, as follows
    var http = require('http');
    var count = 0;
    http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        count += 1;
        console.log('count: ' + count + ': ' + req.url); 
        res.end('Hello World ' + count + '\n');
        }).listen(8080, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:8080/');

    You can then verify the log at the console

    $ node http.js
    Server running at http://127.0.0.1:8080/
    count: 1: /
    count: 2: /favicon.ico
    count: 3: /
    count: 4: /favicon.ico
    count: 5: /
    count: 6: /favicon.ico
  4. To not count the favicon request – we can add a conditional test to the http handler function against the req.urland return 404 for favicon.
    var http = require('http');
    var count = 0;
    http.createServer(function (req, res) {
        if (req.url == '/favicon.ico') {
    	res.writeHead(404, {'Content-type' : 'text/plain'});
    	res.end('not found'); 
        } else {
    	res.writeHead(200, {'Content-Type': 'text/plain'});
    	count += 1;
    	console.log('count: ' + count + ': ' + req.url);
    	res.end('Hello World ' + count + '\n');
        }
        }).listen(8080, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:8080/');

That’s it – your counting NodeJS hello world program.

 

Share

1 ping

  1. NodeJS Serving Static Files | Continuous Learning

    [...] Continuous Learning My journey in life, technology, entrepreneurship, and learning Skip to content HomeAboutAsynchronous IOBooksC ProgrammingContainer SoilHTML5Multi-Threaded ProgrammingNodeJSPurely Functional Data StructuresScheme Interpreter and Compiler ResearchSignalsSoftwareThread PoolThread SynchronizationThread-Safe/Lockless Data StructuresVirtual Machine ← NodeJS Hello World [...]

Leave a Reply to NodeJS Serving Static Files | Continuous Learning Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

To use reCAPTCHA you must get an API key from https://www.google.com/recaptcha/admin/create