资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

node.js学习笔记之读文件

 
直接读硬盘上aa.txt内容
var  http  =  require('http');
var  optfile  =  require('./models/optfile');
http.createServer(function  (request,  response)  {
    response.writeHead(200,  {'Content-Type':  'textnode.js学习笔记之读文件ml;  charset=utf-8'});
    if(request.url!=="/favicon.ico"){  //清除第2此访问
        console.log('访问');
        response.write('hello,world');
        //optfile.readfile("G:node.js学习笔记之读文件\\www\\nodejs\\one\\models\\aa.txt");
        optfile.readfileSync("G:node.js学习笔记之读文件\\www\\nodejs\\one\\models\\aa.txt");
        response.end('hell,世界');//不写则没有http协议尾
    }
}).listen(8000);
console.log('Server  running  at  http://127.0.0.1:8000/');

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;                
    }
}

潮安网站建设公司创新互联公司,潮安网站设计制作,有大型网站制作公司丰富经验。已为潮安1000多家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的潮安做网站的公司定做!

教程学习地址:
node.js学习笔记之读文件http://study.163.com/course/introduction/1003228034.htm#/courseDetail


网页标题:node.js学习笔记之读文件
文章分享:http://cdkjz.cn/article/gcsooo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220