Q:

Node.js server not working

I'm new too node.js and going with tutorials to get the basics
sys = require("sys");  
var http = require("http");  
var url = require("url");
var fs=require("fs");

http.createServer(function(request,response){  
    sys.puts("I got kicked");   
    //var _get = url.parse(request.url, true).query;
    request.on("end", function () {

      fs.readFile("test.txt", 'utf-8', function (error, data) {
         // Write headers.
         response.writeHead(200, {'Content-Type': 'text/plain'});
         // Increment the number obtained from file.
         data = parseInt(data) + 1;
         // Write incremented number to file.
         fs.writeFile('test.txt', data);
         // End response with some nice message.
        response.end('This page was refreshed ' + data + ' times!'); 
      });

   });

    //response.write("Hello World");  
    //response.end('Here is your data: ' + _get['data']);  
}).listen(8080);  
sys.puts("Server Running on 8080");

Got this code from http://net.tutsplus.com/tutorials/javascript-ajax/node-js-for-beginners/
but it's not working and I get "No data received" error from browser.
seriously obsessed I want to know whats wrong with it. if i put response.end() out of request.on() it'll work but will not recognize data object...
help!

A:

You don't want to use request.on but you do want to put response.end in the readFile callback, otherwise it'll return before the data is read out of it:
var sys = require("sys");
var http = require("http");
var url = require("url");
var fs=require("fs");

http.createServer(function(request,response){
    sys.puts("I got kicked");
    //var _get = url.parse(request.url, true).query;
    fs.readFile("test.txt", 'utf-8', function (error, data) {
       // Write headers.
       response.writeHead(200, {'Content-Type': 'text/plain'});
       // Increment the number obtained from file.
       data = parseInt(data) + 1;
       // Write incremented number to file.
       fs.writeFile('test.txt', data);
       // End response with some nice message.
       response.end('This page was refreshed ' + data + ' times!');
    });

}).listen(8080);
sys.puts("Server Running on 8080");

And as @chris noted, your counter will be incremented by 2 every time.

