javascript - what is the executing sequence of "switch case" in node js - Stack Overflow

I'm new to node.js, I met a strange issue when run a simple sample.var http = require("http&q

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745323613a4622556.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信