2016-09-17 127 views
-1

我怎样才能从内容:的NodeJS READFILE获取内容

fs.readFile('./access.log', function read(err, data) { 
var content = data.toString(); 
}); 

我需要在其他功能使用它。

当我tryied这样的:

var content; 
fs.readFile('./access.log', function read(err, data) { 
content = data.toString(); 
}); 

console.log("Content: " + content); 

这表明内容是不确定的。

什么是最佳解决方案?

+1

你不能把异步函数,就好像它们是同步的。关于节点已经讨论过这一点。尝试搜索这里和谷歌,你会发现很多,*很多*的结果。 – mscdex

+0

./access.log文件位置不可访问 –

+0

我检查,当我把console.log(内容)功能,它显示此文件的内容 – divine

回答

0

的soluton是

var text = fs.readFileSync('./access.log', 'utf8');