node.js学习笔记之读文件
Posted 老何de技术日志
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node.js学习笔记之读文件相关的知识,希望对你有一定的参考价值。
直接读硬盘上aa.txt内容
var http = require(‘http‘);
var optfile = require(‘./models/optfile‘);
http.createServer(function (request, response) {
response.writeHead(200, {‘Content-Type‘: ‘text
if(request.url!=="/favicon.ico"){ //清除第2此访问
console.log(‘访问‘);
response.write(‘hello,world‘);
//optfile.readfile("G:
optfile.readfileSync("G:
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;
}
}
教程学习地址:
http://study.163.com/course/introduction/1003228034.htm#/courseDetail
![技术分享](http://hezudao.blog.51cto.com/e/u261/themes/default/images/spacer.gif)
以上是关于node.js学习笔记之读文件的主要内容,如果未能解决你的问题,请参考以下文章