直接讀硬盤上aa.txt內容
var http = require('http');
var optfile = require('./models/optfile');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text
![](http://static.javashuo.com/static/loading.gif)
ml; charset=utf-8'});
if(request.url!=="/favicon.ico"){ //清除第2此訪問
console.log('訪問');
response.write('hello,world');
//optfile.readfile("G:
![](http://static.javashuo.com/static/loading.gif)
\\www\\nodejs\\one\\models\\aa.txt");
optfile.readfileSync("G:
![](http://static.javashuo.com/static/loading.gif)
\\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;
}
}