admin管理员组

文章数量:1420159

I'm new to node.js, I met a strange issue when run a simple sample.

var http = require("http");
var url = require('url');
var fs = require('fs');

var server = http.createServer(function(request, response){ 
    console.log('Connection');
    var path = url.parse(request.url).pathname;
    console.log(1);
    switch(path){
        case '/':
            response.writeHead(200, {'Content-Type': 'text/html'}); 
            response.write('hello world');
            break;
        case '/socket.html':
            console.log(2);
            fs.readFile(__dirname + path, function(error, data){
                if (error){
                    response.writeHead(404);
                    response.write("opps this doesn't exist - 404");
                    console.log(3);
                }
                else{
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data, "utf8");
                    console.log(4);
                }
            });
            break;
        default:
            response.writeHead(404);
            response.write("opps this doesn't exist - 404");
            break;
    }
    console.log(5);
    response.end(); 
}); 

server.listen(8001); 

when I visit

http://localhost:8001/socket.html

backend log output as following:

    Connection
    1
    2
    5
    4
    Connection
    1
    5

In the browser, there is no output, the source of html is empty. Notice the log, I guess this maybe the response has already closed after log print "5", before log print "4".

I can't understand why the log sequence is not "1245", could somebody explain this to me?

I'm new to node.js, I met a strange issue when run a simple sample.

var http = require("http");
var url = require('url');
var fs = require('fs');

var server = http.createServer(function(request, response){ 
    console.log('Connection');
    var path = url.parse(request.url).pathname;
    console.log(1);
    switch(path){
        case '/':
            response.writeHead(200, {'Content-Type': 'text/html'}); 
            response.write('hello world');
            break;
        case '/socket.html':
            console.log(2);
            fs.readFile(__dirname + path, function(error, data){
                if (error){
                    response.writeHead(404);
                    response.write("opps this doesn't exist - 404");
                    console.log(3);
                }
                else{
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data, "utf8");
                    console.log(4);
                }
            });
            break;
        default:
            response.writeHead(404);
            response.write("opps this doesn't exist - 404");
            break;
    }
    console.log(5);
    response.end(); 
}); 

server.listen(8001); 

when I visit

http://localhost:8001/socket.html

backend log output as following:

    Connection
    1
    2
    5
    4
    Connection
    1
    5

In the browser, there is no output, the source of html is empty. Notice the log, I guess this maybe the response has already closed after log print "5", before log print "4".

I can't understand why the log sequence is not "1245", could somebody explain this to me?

Share Improve this question asked Jul 26, 2013 at 8:18 yuyue007yuyue007 1,2793 gold badges16 silver badges25 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

The fs.readFile method is asynchronous. When you call it, execution continues immediately after it (at the break statement), and then proceeds to the console.log(5), and the response.end() calls.

Therefore, response.end() is being called before the fs.readFile callback gets a chance to execute. You need to move the response.end() call inside the callback.

本文标签: javascriptwhat is the executing sequence of quotswitch casequot in node jsStack Overflow