Building a simple joke site with nodejs – Part 1 – Hello World

If you’ve heard about nodejs and always wanted to get started with it, look no further! I’ll walk you through the basics of getting set-up and building a super simple joke website from start to finish, using my current website happypolack.com as a goal to get to.

NOTE: These tutorials were written with node version 0.12.4 and npm version 2.10.1. Type “node -v” and “npm -v” to see your versions

Why nodejs?

NodeJS was engineered from the ground up to be excellent at server event driven websites and services. It’s also much easier to develop in (once you get the hang of it), since you can use the same language that you use for the front-end (javascript!). Finally, the node package manager (NPM) allows you to simply and easily utilize a world of different open source and free code bases instantly!

Installation

For Windows users

Since I still use windows at home, I’m going to first walk through these instructions assuming you are running windows as well.

  1. Download the (msi) installer from the nodejs website
  2. NodeJS Website

  3. For the Install directory, I chose something a bit easier to remember:
    D:\nodejs
    
  4. That’s it! Now, when you want to run a server, you can simply type “node .js” in your command prompt.

For Mac OS X users

A fantastic command line package manager for mac os is called brew. Once you install that, you can easily install node by simply typing:

brew install node

That’s it!

A helloworld “website”

  • Create a file called “helloworld.js” in an easy to remember directory (I chode D:\nodescripts) and add the following content to it:

    var http = require('http');
    
    http.createServer(function (req, res) {
    	res.writeHead(200, {'Content-Type': 'text/html'});
    	res.end('Hello World\n');
    }).listen(80, "127.0.0.1");
    
    console.log('Server running at http://127.0.0.1:80/');
    
  • Now open up the command prompt and navigate to your file:
    cd D:\nodescripts
    
  • To start your local web server, now type:
    node helloworld.js
    
  • You should see the message

    Server running at http://127.0.0.1:80
    
  • Open your browser and navigate to http://127.0.0.1 (also known as http://localhost)
  • Viola!

Summary

See how easy that was? Now you’re ready to get started with nodejs. Stay tuned for part 2.



Share this post


facebooktwittergoogle_plusredditpinterestlinkedinmail

2 Responses to “Building a simple joke site with nodejs – Part 1 – Hello World”

  1. Building Happy Polack with nodejs tutorial - Part 2 Says:

    […] Programmer, Engineer, Researcher. « Getting started with NodeJS – Part 1 – Hello World […]

  2. Building a simple joke site with nodejs – Part 3 – Expressjs | Phillip Napieralski Says:

    […] Part 1 – Hello World Part 2 – Adding our flat file “database” […]