node.js学习笔记之读文件
直接读硬盘上aa.txt内容
varhttp=require('http');
varoptfile=require('./models/optfile');
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'textml;charset=utf-8'});
if(request.url!=="/favicon.ico"){//清除第2此访问
console.log('访问');
response.write('hello,world');
//optfile.readfile("G:\\www\\nodejs\\one\\models\\aa.txt");
optfile.readfileSync("G:\\www\\nodejs\\one\\models\\aa.txt");
response.end('hell,世界');//不写则没有http协议尾
}
}).listen(8000);
console.log('Serverrunningathttp://127.0.0.1:8000/');
optfile.js
//两种读取文件方式,同步和异步
varfs=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){//同步读取
vardata=fs.readFileSync(path,'utf-8');
//console.log(data);
console.log("同步方法执行完毕");
returndata;
}
}
教程学习地址:
http://study.163.com/course/introduction/1003228034.htm#/courseDetail
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。