Node.js 这是NodeJS的正确行为吗?
我使用Node.js 这是NodeJS的正确行为吗?,node.js,Node.js,我使用fs.readdir获得了一个文件列表,并获取了试图读取其内容的前5个文件 files.slice(0,5).forEach(function(item){ console.log(item); readContent(path+"/"+item); } ); function readContent(filename)
fs.readdir
获得了一个文件列表,并获取了试图读取其内容的前5个文件
files.slice(0,5).forEach(function(item){
console.log(item);
readContent(path+"/"+item);
}
);
function readContent(filename)
{
var fs= require("fs");
fs.readFile(filename, 'utf8', function(err, data)
{
console.log(data);
});
}
<script type="text/javascript"> //<![CDATA[ window.location.replace('/#!/LovelyThang80/status/28965131362770944'); //]]> </script>
<script type="text/javascript"> //<![CDATA[ (function(g){var c=g.location.href.split("#!");if(c[1]){g.location.replace(g.HBR = (c[0].replace(/\/*$/, "") + "/" + c[1].replace(/^\/*/, "")));}else return true})(window); //]]> </script>
<script type="text/javascript" charset="utf-8">
if (!twttr) {
var twttr = {}
}
// Benchmarking load time.
// twttr.timeTillReadyUnique = '1309338925-32926-11310';
// twttr.timeTillReadyStart = new Date().getTime();
</script>
<script type="text/javascript"> //<![CDATA[ var page={};var onCondition=function(D,C,A,B){D=D;A=A?Math.min(A,5):5;B=B||100;if(D()){C()}else{if(A>1){setTimeout(function(){onCondition(D,C,A-1,B)},B)}}}; //]]> </script>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta content="en-us" http-equiv="Content-Language" /> <meta content="Chef salad is calling my name, I'm so hungry!" name="description" /> <meta content="no" http-equiv="imagetoolbar" /> <meta content="width = 780" name="viewport" /> <meta content="4FTTxY4uvo0RZTMQqIyhh18HsepyJOctQ+XTOu1zsfE=" name="verify-v1" /> <meta content="1" name="page" /> <meta content="NOODP" name="robots" /> <meta content="n" name="session-loggedin" /> <meta content="LovelyThang80" name="page-user-screen_name" />
<title id="page_title">Twitter / Miss ImpressiveAngel: Chef salad is calling my n ...</title>
<link href="http://a1.twimg.com/a/1309298903/images/twitter_57.png" rel="apple-touch-icon" /> <link href="/oexchange.xrd" rel="http://oexchange.org/spec/0.8/rel/related-target" type="application/xrd+xml" /> <link href="http://a3.twimg.com/a/1309298903/images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="http://a3.twimg.com/a/1309298903/stylesheets/twitter.css?1309198825" media="screen" rel="stylesheet" type="text/css" /> <lin
但是它先打印文件名,然后只打印内容,而不是像我预期的那样“filename->content”。我不熟悉NodeJS,这是异步特性的一部分吗?还是我做错了什么
28965131362770944.txt
28965131668946944.txt
28965131803168769.txt
28965131991912448.txt
28965132189040641.txt
<script type="text/javascript"> //<![CDATA[ window.location.replace('/#!/LovelyThang80/status/28965131362770944'); //]]> </script>
<script type="text/javascript"> //<![CDATA[ (function(g){var c=g.location.href.split("#!");if(c[1]){g.location.replace(g.HBR = (c[0].replace(/\/*$/, "") + "/" + c[1].replace(/^\/*/, "")));}else return true})(window); //]]> </script>
<script type="text/javascript" charset="utf-8">
if (!twttr) {
var twttr = {}
}
// Benchmarking load time.
// twttr.timeTillReadyUnique = '1309338925-32926-11310';
// twttr.timeTillReadyStart = new Date().getTime();
</script>
<script type="text/javascript"> //<![CDATA[ var page={};var onCondition=function(D,C,A,B){D=D;A=A?Math.min(A,5):5;B=B||100;if(D()){C()}else{if(A>1){setTimeout(function(){onCondition(D,C,A-1,B)},B)}}}; //]]> </script>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta content="en-us" http-equiv="Content-Language" /> <meta content="Chef salad is calling my name, I'm so hungry!" name="description" /> <meta content="no" http-equiv="imagetoolbar" /> <meta content="width = 780" name="viewport" /> <meta content="4FTTxY4uvo0RZTMQqIyhh18HsepyJOctQ+XTOu1zsfE=" name="verify-v1" /> <meta content="1" name="page" /> <meta content="NOODP" name="robots" /> <meta content="n" name="session-loggedin" /> <meta content="LovelyThang80" name="page-user-screen_name" />
<title id="page_title">Twitter / Miss ImpressiveAngel: Chef salad is calling my n ...</title>
<link href="http://a1.twimg.com/a/1309298903/images/twitter_57.png" rel="apple-touch-icon" /> <link href="/oexchange.xrd" rel="http://oexchange.org/spec/0.8/rel/related-target" type="application/xrd+xml" /> <link href="http://a3.twimg.com/a/1309298903/images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="http://a3.twimg.com/a/1309298903/stylesheets/twitter.css?1309198825" media="screen" rel="stylesheet" type="text/css" /> <lin
<script type="text/javascript"> //<![CDATA[ window.location.replace('/#!/LovelyThang80/status/28965131362770944'); //]]> </script>
<script type="text/javascript"> //<![CDATA[ (function(g){var c=g.location.href.split("#!");if(c[1]){g.location.replace(g.HBR = (c[0].replace(/\/*$/, "") + "/" + c[1].replace(/^\/*/, "")));}else return true})(window); //]]> </script>
<script type="text/javascript" charset="utf-8">
if (!twttr) {
var twttr = {}
}
// Benchmarking load time.
// twttr.timeTillReadyUnique = '1309338925-32926-11310';
// twttr.timeTillReadyStart = new Date().getTime();
</script>
<script type="text/javascript"> //<![CDATA[ var page={};var onCondition=function(D,C,A,B){D=D;A=A?Math.min(A,5):5;B=B||100;if(D()){C()}else{if(A>1){setTimeout(function(){onCondition(D,C,A-1,B)},B)}}}; //]]> </script>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta content="en-us" http-equiv="Content-Language" /> <meta content="Chef salad is calling my name, I'm so hungry!" name="description" /> <meta content="no" http-equiv="imagetoolbar" /> <meta content="width = 780" name="viewport" /> <meta content="4FTTxY4uvo0RZTMQqIyhh18HsepyJOctQ+XTOu1zsfE=" name="verify-v1" /> <meta content="1" name="page" /> <meta content="NOODP" name="robots" /> <meta content="n" name="session-loggedin" /> <meta content="LovelyThang80" name="page-user-screen_name" />
<title id="page_title">Twitter / Miss ImpressiveAngel: Chef salad is calling my n ...</title>
<link href="http://a1.twimg.com/a/1309298903/images/twitter_57.png" rel="apple-touch-icon" /> <link href="/oexchange.xrd" rel="http://oexchange.org/spec/0.8/rel/related-target" type="application/xrd+xml" /> <link href="http://a3.twimg.com/a/1309298903/images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="http://a3.twimg.com/a/1309298903/stylesheets/twitter.css?1309198825" media="screen" rel="stylesheet" type="text/css" /> <lin
//
//
如果(!twttr){
var twttr={}
}
//基准测试加载时间。
//twttr.timeTillReadyUnique='1309338925-32926-11310';
//twttr.timeTillReadyStart=new Date().getTime();
//1) {setTimeout(function(){onCondition(D,C,A-1,B)},B)};//]>
推特/印象派天使小姐:厨师沙拉正在打电话给我的n。。。
是的,这是预期的行为。Node.js中的所有IO操作都是异步完成的。这意味着console.log(item)
将立即执行,但读取内容将在稍后执行,因为它是异步的。因此,主线程首先打印所有文件名,然后打印其内容
files.slice(0,5).forEach(function(item){
console.log(item);
readContent(path+"/"+item);
}
);
function readContent(filename)
{
var fs= require("fs");
fs.readFile(filename, 'utf8', function(err, data)
{
console.log(data);
});
}
<script type="text/javascript"> //<![CDATA[ window.location.replace('/#!/LovelyThang80/status/28965131362770944'); //]]> </script>
<script type="text/javascript"> //<![CDATA[ (function(g){var c=g.location.href.split("#!");if(c[1]){g.location.replace(g.HBR = (c[0].replace(/\/*$/, "") + "/" + c[1].replace(/^\/*/, "")));}else return true})(window); //]]> </script>
<script type="text/javascript" charset="utf-8">
if (!twttr) {
var twttr = {}
}
// Benchmarking load time.
// twttr.timeTillReadyUnique = '1309338925-32926-11310';
// twttr.timeTillReadyStart = new Date().getTime();
</script>
<script type="text/javascript"> //<![CDATA[ var page={};var onCondition=function(D,C,A,B){D=D;A=A?Math.min(A,5):5;B=B||100;if(D()){C()}else{if(A>1){setTimeout(function(){onCondition(D,C,A-1,B)},B)}}}; //]]> </script>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta content="en-us" http-equiv="Content-Language" /> <meta content="Chef salad is calling my name, I'm so hungry!" name="description" /> <meta content="no" http-equiv="imagetoolbar" /> <meta content="width = 780" name="viewport" /> <meta content="4FTTxY4uvo0RZTMQqIyhh18HsepyJOctQ+XTOu1zsfE=" name="verify-v1" /> <meta content="1" name="page" /> <meta content="NOODP" name="robots" /> <meta content="n" name="session-loggedin" /> <meta content="LovelyThang80" name="page-user-screen_name" />
<title id="page_title">Twitter / Miss ImpressiveAngel: Chef salad is calling my n ...</title>
<link href="http://a1.twimg.com/a/1309298903/images/twitter_57.png" rel="apple-touch-icon" /> <link href="/oexchange.xrd" rel="http://oexchange.org/spec/0.8/rel/related-target" type="application/xrd+xml" /> <link href="http://a3.twimg.com/a/1309298903/images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="http://a3.twimg.com/a/1309298903/stylesheets/twitter.css?1309198825" media="screen" rel="stylesheet" type="text/css" /> <lin
如果要打印“文件名内容”、“文件名内容”等序列,需要为readContent()
方法指定文件名,然后将其记录在readFile()
方法中。像这样
<script type="text/javascript"> //<![CDATA[ window.location.replace('/#!/LovelyThang80/status/28965131362770944'); //]]> </script>
<script type="text/javascript"> //<![CDATA[ (function(g){var c=g.location.href.split("#!");if(c[1]){g.location.replace(g.HBR = (c[0].replace(/\/*$/, "") + "/" + c[1].replace(/^\/*/, "")));}else return true})(window); //]]> </script>
<script type="text/javascript" charset="utf-8">
if (!twttr) {
var twttr = {}
}
// Benchmarking load time.
// twttr.timeTillReadyUnique = '1309338925-32926-11310';
// twttr.timeTillReadyStart = new Date().getTime();
</script>
<script type="text/javascript"> //<![CDATA[ var page={};var onCondition=function(D,C,A,B){D=D;A=A?Math.min(A,5):5;B=B||100;if(D()){C()}else{if(A>1){setTimeout(function(){onCondition(D,C,A-1,B)},B)}}}; //]]> </script>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta content="en-us" http-equiv="Content-Language" /> <meta content="Chef salad is calling my name, I'm so hungry!" name="description" /> <meta content="no" http-equiv="imagetoolbar" /> <meta content="width = 780" name="viewport" /> <meta content="4FTTxY4uvo0RZTMQqIyhh18HsepyJOctQ+XTOu1zsfE=" name="verify-v1" /> <meta content="1" name="page" /> <meta content="NOODP" name="robots" /> <meta content="n" name="session-loggedin" /> <meta content="LovelyThang80" name="page-user-screen_name" />
<title id="page_title">Twitter / Miss ImpressiveAngel: Chef salad is calling my n ...</title>
<link href="http://a1.twimg.com/a/1309298903/images/twitter_57.png" rel="apple-touch-icon" /> <link href="/oexchange.xrd" rel="http://oexchange.org/spec/0.8/rel/related-target" type="application/xrd+xml" /> <link href="http://a3.twimg.com/a/1309298903/images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="http://a3.twimg.com/a/1309298903/stylesheets/twitter.css?1309198825" media="screen" rel="stylesheet" type="text/css" /> <lin
files.slice(0,5).forEach(function(item){
readContent(path+"/"+item, item);
});
function readContent(filename, item) {
var fs = require("fs");
fs.readFile(filename, 'utf8', function(err, data) {
console.log(item);
console.log(data);
});
}
另外,有一个选项可以调用同步读取文件的fs.readFileSync()
。我强烈建议您不要在高效代码中使用它,因为在读取文件时,它将停止处理其他传入请求。同步方法通常在应用程序初始化期间使用,但在运行时,当应用程序服务于请求时,则不会使用同步方法。您将需要包含更多的程序,特别是readContent()函数。关于下面的答案/和您的问题,这很酷。谢谢有没有强制同步的方法?好的,如果我用jQuery/javascript的方式来考虑,那么我完全可以理解背后的原因。
<script type="text/javascript"> //<![CDATA[ window.location.replace('/#!/LovelyThang80/status/28965131362770944'); //]]> </script>
<script type="text/javascript"> //<![CDATA[ (function(g){var c=g.location.href.split("#!");if(c[1]){g.location.replace(g.HBR = (c[0].replace(/\/*$/, "") + "/" + c[1].replace(/^\/*/, "")));}else return true})(window); //]]> </script>
<script type="text/javascript" charset="utf-8">
if (!twttr) {
var twttr = {}
}
// Benchmarking load time.
// twttr.timeTillReadyUnique = '1309338925-32926-11310';
// twttr.timeTillReadyStart = new Date().getTime();
</script>
<script type="text/javascript"> //<![CDATA[ var page={};var onCondition=function(D,C,A,B){D=D;A=A?Math.min(A,5):5;B=B||100;if(D()){C()}else{if(A>1){setTimeout(function(){onCondition(D,C,A-1,B)},B)}}}; //]]> </script>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <meta content="en-us" http-equiv="Content-Language" /> <meta content="Chef salad is calling my name, I'm so hungry!" name="description" /> <meta content="no" http-equiv="imagetoolbar" /> <meta content="width = 780" name="viewport" /> <meta content="4FTTxY4uvo0RZTMQqIyhh18HsepyJOctQ+XTOu1zsfE=" name="verify-v1" /> <meta content="1" name="page" /> <meta content="NOODP" name="robots" /> <meta content="n" name="session-loggedin" /> <meta content="LovelyThang80" name="page-user-screen_name" />
<title id="page_title">Twitter / Miss ImpressiveAngel: Chef salad is calling my n ...</title>
<link href="http://a1.twimg.com/a/1309298903/images/twitter_57.png" rel="apple-touch-icon" /> <link href="/oexchange.xrd" rel="http://oexchange.org/spec/0.8/rel/related-target" type="application/xrd+xml" /> <link href="http://a3.twimg.com/a/1309298903/images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="http://a3.twimg.com/a/1309298903/stylesheets/twitter.css?1309198825" media="screen" rel="stylesheet" type="text/css" /> <lin