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 01 Answer
Reset to default 4The 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
版权声明:本文标题:javascript - what is the executing sequence of "switch case" in node js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745323612a2653491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论