我使用fs.readdir
获得了一个文件列表,并取前5个文件尝试读取其内容。这是NodeJS的正确行为吗?
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);
});
}
但它打印文件名,然后只打印内容,而不是“文件名 - >内容”,如我所料。我是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
你将需要包含更多的程序,特别是readContent()函数。 –
代码更新:) – cherhan
关于以下答案/和您的问题 http://nodejs.org/api/fs.html#fs_fs_readfilesync_filename_options – xhallix