optfile.js
//-------------optfile.js-------------------------
var fs = require('fs');
module.exports = {
readfileSync: function (path) { //同步读取
var data = fs.readFileSync(path, 'utf-8');
console.log(data);
console.log("同步方法执行完毕");
return data;
},
readfile: function (path) { //异步执行
fs.readFile(path, function (err, data) {
if (err) {
console.log(err);
} else {
console.log(data.toString());
}
});
console.log("异步方法执行完毕");
}
}
//-----------n5_readfile----读取文件---------------------
/*
* 单线程异步
*/
var http = require('http');
var optfile = require('./model/optfile.js');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
if (request.url !== "/favicon.ico") { //清除第2此访问
//optfile.readfileSync('./view/01.txt');//同步执行
optfile.readfile('./view/01.txt');
response.end(''); //不写则没有http协议尾
console.log("主程序执行完毕!");
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');