Saturday, March 31, 2012

Node.JS Sample Application On Windows

I am I'm kind of new to  Node.js   world. I took nearly a week reading tutorials and downloading samples to say "Hello World" to Node.Js and to Socket.io. Finally today I have achieved it :). Let me document what I have done here before I forget all the things. Here I'm going to describe the first step running first Node server on windows. I will go through step by step starting from the downloading  Node.js .

Download and Install Node.js
We can download Node.JS server for windows from here . Nothing to configure... Just download and install it. You can check whether you have installed it correctly using command prompt. Type node and press Enter you will get the node prompt.

Host the sample script
This is the sample code for  hello world server.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Create new text file and save above code as sample.js

Run Sample Server
Then go to your folder (using command prompt) which has the sample.js file and execute it using node by executing command node sample.js


Access It Using Browser
Open your browser and enter the address we have given in the sample.js file.

That's It !
I will go more detail in to node.js and socket.io in the next post :).

4 comments:

  1. this is awesome example

    ReplyDelete
  2. Thanks,
    It is really helpful to me.

    ReplyDelete
  3. Hi nice and useful article. However, it ios working fine when I access the url on the same machine, when browsing from another machine i get "This page can't be displayed in spite of the fact that I cnaged 127.0.0.1 by machine IP

    ReplyDelete
    Replies
    1. You may need to open connections form the external http requests. In windows you may need to open the firewall. I'm not sure how it will work on IOS.

      Delete