Nodejs_day01

helloworld.txt的内容:

我是nodejs

阻塞式调用

var fs = require('fs');

var data = fs.readFileSync('helloworld.txt');

console.log(data.toString());

console.log("程序执行完毕!");

执行结果:

我是nodejs

程序执行完毕!

非阻塞式调用:

var fs = require('fs');

fs.readFile('helloworld.txt',function(err,data){

  if(err){return console.error(err);}

  console.log(data.toString());

});

console.log("程序执行完毕!");

执行结果:

程序执行完毕!

我是nodejs

原文地址:https://www.cnblogs.com/liyajie/p/4808144.html