Jamie Hutber
Jamie Hutber

Reputation: 28076

Node.js server won't start on windows 7 64-bit

It seems I have something very wrong with my set up, I currently have node -v //v0.10.29 installed on my windows 7 64Bit machine.

// Include http module.
var http = require("http");

http.createServer(function (request, response) {
    request.on("end", function () {
        response.writeHead(200, {
            'Content-Type': 'text/plain'
        });
        response.end('Hello HTTP!');
    });
}).listen(8080);

This is the file I have running via node http.js. There is nothing else in the folder other than this file.

Running this little badboy: node-inspector & node --debug http.js the following is returned in the cmd:

Node Inspector v0.7.4
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.

I can navigate to http://127.0.0.1:8080 and also the suggested url, navigating with debug?etc gives me a console, similar to chrome's developers tools.

Navigating to the url without the params gives me Cannot GET / with the request 404'in

With said headers:

Remote Address:127.0.0.1:8080
Request URL:http://127.0.0.1:8080/
Request Method:GET
Status Code:404 Not Found
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:127.0.0.1:8080
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like  Chrome/35.0.1916.153 Safari/537.36
Response Headersview source
Connection:keep-alive
Content-Type:text/html
Date:Thu, 17 Jul 2014 23:07:13 GMT
Transfer-Encoding:chunked
X-Powered-By:Express

So how can i connect to a simple http server in node.js and windows 7

Upvotes: 0

Views: 218

Answers (1)

Yuan
Yuan

Reputation: 1157

// Include http module.
var http = require("http");

http.createServer(function (request, response) {
    request.on("end", function () {
        // ...
    }); 
    response.writeHead(200, {
        'Content-Type': 'text/plain'
    });
    response.end('Hello HTTP!');
}).listen(8080);

To understand the problem, we can do some experiments.

Add console logs around responses and request end event

console.log("response start");
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
console.log("response end");
request.on("end", function () {
    console.log("request end");
});

The output:

   response start
   response end
   request end

Then comment out this line of response.end.

console.log("response start");
response.writeHead(200, {'Content-Type': 'text/plain'});
//response.end('Hello World\n');
console.log("response end");
request.on("end", function () {
    console.log("request end");
});

Now the request is stuck. The end event is never emitted.

response start
response end

So the conclusion is end event of request only be fired after response.end is done. You shouldn't place your response in the body of request's end event.

Upvotes: 3

Related Questions