Hello World with Node JS

My first experience with web pages was through HTML. I remember I was completely baffled by how seemingly cryptic text transformed itself into structured data.

A long time has passed since then. I’ve had some experience working on various parts of the web stack; but so far all this has been through the use of Microsoft technologies, my interactions with the open source world have been few and far between. And although I love the MS stack, of late, I’ve felt the need to know what’s out there; chocolatey, Node Js, NPM, Mongo, Heroku, Shippable, etc. The sheer volume of names is overwhelming, let alone choosing the right tool for the job.

This is my endeavor to document the process of adopting a new technology stack to build a functional app. This is what I plan to use:

  • Node. Js – Server side coding language
  • WebStorm – JavaScript IDE
  • Mongo – Database
  • Mongoose – ORM      layer
  • Shippable – CI      platform
  • Heroku – Cloud

I’m an absolute N00B and my intention here is that the hurdles I encounter are probably what others in my position would face. There’s a lot of great material on all the above topics out there – THIS IS NOT THAT. This is exactly what I say it is – a newbie’s experience at using a new technology stack.

The ubiquitous Hello World application

I am working on a Windows 8 machine.

Setting up the environment

  • Go to http://nodejs.org/ and install the latest version.
  • Now navigate to the Node installation folder – in my case it was C:\Users\viswka\Documents\nodejs – and install express (http://expressjs.com/) by running the following command:

npm install -g express@2.5.8

Image

The npm command is run with the “-g” flavor. Read more about global vs. local installations here: http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/ .

  • Express is the HTTP server (there are others as well) used for Node development.
    *Using Node, means that server administration as well as application development is the responsibility of the application developer and both these can be achieved using JavaScript
  • At this point of time, I believe I have everything I need to create the Hello World app – everything but an IDE – Visual Studio has spoiled me rotten. Here’s a good blog on the choice of IDE available for Windows developers – http://beletsky.net/2012/06/starting-up-nodejs-development-on.html . I decided to use Web Storm (the free version for now) since I’ve used other JetBrains products in the past and am comfortable with it.

Create the ‘Hello World’ application

  • Start WebStorm and create a new project; I decided to go with the ‘Empty project’ template for now – it looks like there are others like ‘Boilerplate and Express, I’ll delve into those later.
  • Create a new server.js file and add the following code, which will create a basic HTTP server.
var http = require(“http”);
http.createServer(function(request, response) {
response.writeHead(200, {“Content-Type”: “text/plain”});
response.write(“Hello World”);
response.end();
}).listen(8888);

Publishing a gist

Right click the project and click on “Create Gist…”. If you have a github account you will see the code pushed to github; if not create a github account and repeat this step.

You can access my code here : https://gist.github.com/ViswanathKari/9166632

Advertisements