node.js 学习五 之 文件操作

node.js 中有同步读取文件和异步读取文件的区别

同步读取就是顺序进行,文件读取操作不进行完就不进行下一步操作
异步读取就是文件读取操作时,另起了一个线程,继续进行

异步的方法函数最后一个参数为回调函数,回调函数的第一个参数包含了错误信息(error)。

文件读取

05_readfile.js

var http = require('http');
var optfile = require('./models/optfile');
http.createServer(function(request, response) {
    response.writeHead(200, { 'Content-Type': 'text/html;  charset=utf-8' });
    if (request.url !== "/favicon.ico") { //清除第2此访问
        console.log('访问');
        response.write('hello,world');
        var path = "D:\\learn\\nodejs\\05\\models\\test.txt"
        // optfile.readfile(path);
        optfile.readfileSync(path);
        response.end('hell,世界'); //不写则没有http协议尾
    }
}).listen(8000);
console.log('Server  running  at  http://127.0.0.1:8000/');

./models/optfile.js

var  fs=  require('fs');
module.exports={
    readfile:function(path){          //异步执行
        fs.readFile(path,  function  (err,  data)  {
            if  (err)  {
              console.log(err);
            }else{
              console.log(data.toString());
            }
        });
        console.log("异步方法执行完毕");
    },
    readfileSync:function(path){      //同步读取
        var  data  =  fs.readFileSync(path,'utf-8');
        console.log(data);
        console.log("同步方法执行完毕");
        return  data;                
    }
}

输出:
同步:

readfile.png

异步:


readfile_sync.png

使用闭包异步读取文件后写到前端

05_readfile.js

var http = require('http');
var optfile = require('./models/optfile');
http.createServer(function(request, response) {
    response.writeHead(200, { 'Content-Type': 'text/html;  charset=utf-8' });
    if (request.url !== "/favicon.ico") { //清除第2此访问
        // 闭包
        function recall(data) {
            response.write(data)
            response.end('hell,世界')
        }

        console.log('访问');
        response.write('hello,world');
        var path = "D:\\learn\\nodejs\\05\\models\\test.txt"
        optfile.readfile(path, recall);
        // optfile.readfileSync(path);

        // response.end('hell,世界'); //不写则没有http协议尾
    }
}).listen(8000);
console.log('Server  running  at  http://127.0.0.1:8000/');

./models/optfile.js

var  fs=  require('fs');
module.exports={
    readfile:function(path, recall){          //异步执行
        fs.readFile(path,  function  (err,  data)  {
            if  (err)  {
              console.log(err);
            }else{
              console.log(data.toString());
              recall(data)
            }
        });
        console.log("异步方法执行完毕");
    },
    readfileSync:function(path){      //同步读取
        var  data  =  fs.readFileSync(path,'utf-8');
        console.log(data);
        console.log("同步方法执行完毕");
        return  data;                
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • topics: 1.The Node.js philosophy 2.The reactor pattern 3....
    宫若石阅读 1,132评论 0 1
  • Node.js是目前非常火热的技术,但是它的诞生经历却很奇特。 众所周知,在Netscape设计出JavaScri...
    Myselfyan阅读 4,103评论 2 58
  • Node.js是目前非常火热的技术,但是它的诞生经历却很奇特。 众所周知,在Netscape设计出JavaScri...
    w_zhuan阅读 3,642评论 2 41
  • 异步编程对JavaScript语言太重要。Javascript语言的执行环境是“单线程”的,如果没有异步编程,根本...
    呼呼哥阅读 7,336评论 5 22
  • 1.链接的标签:<a> 使用方法: 点击提示 例如: 百度一下 2.target属性:表示链接的跳出形式 例如: ...
    xiadada阅读 1,370评论 0 0