node.js 读文件

同步读取文件

nj_file.js

var  http  =  require('http');  
var openfile = require('./model/openfile');

http.createServer(function  (request,  response)  {  
	 response.writeHead(200,  {'Content-Type':  'text/html;  charset=utf-8'});  
 	  if(request.url!=="/favicon.ico"){ 
 	 openfile.readfileSync('./view/login.html');
          console.log('主程序执行完毕');
          response.end('ok');
  }
}).listen(8000);  
console.log('Server  running  at  http://127.0.0.1:8000/');  

  openfile.js

var  fs=  require('fs');//node自带的类
module.exports={
    readfileSync:function(path){      //同步读取
        var  data  =  fs.readFileSync(path,'utf-8');
        console.log(data);
        console.log("同步方法执行完毕");
        /*return  data; */             
    }
    
}

 结果

程序会先执行到方法,读取文件之后再跳转主程序执行,同步执行会比较慢的,程序是一步一步执行下去的,比较花时间。

----------------------------------------------------------------------------------------------------

异步读取文件

nj_file.js

var  http  =  require('http');  
var openfile = require('./model/openfile');

http.createServer(function  (request,  response)  {  
	 response.writeHead(200,  {'Content-Type':  'text/html;  charset=utf-8'});  
 	  if(request.url!=="/favicon.ico"){ 
 	 function recall(date){
 	 	response.write(date);
 	 	response.end('ok');
 	 	
 	 }
 	 openfile.readfile('./view/login.html',recall);
      console.log('主程序执行完毕');
     
  }
}).listen(8000);  
console.log('Server  running  at  http://127.0.0.1:8000/');  

  openfile.js

var  fs=  require('fs');//node自带的类
module.exports={
    readfile:function(path,recall){          //异步执行
        fs.readFile(path,  function  (err,  data)  {
            if  (err)  {
              console.log(err);
            }else{
              console.log(data.toString());
              recall(data);
            }
        });
        console.log("异步方法执行完毕");
    }
}

  结果

程序会一步一步的执行下去,读取文件的时候使用异步完成,读完主程序后在加载文件信息,这里用的一个闭包函数完成。

-----------------------------------------------------------------------------------------------------------------------

实现根据路由路径读取不同的文件

 nj_file.js

var  http  =  require('http');  
var url = require('url');
var  router = require('./model/router');

http.createServer(function  (request,  response)  {  
	 response.writeHead(200,  {'Content-Type':  'text/html;  charset=utf-8'});  
 	  if(request.url!=="/favicon.ico"){ 
 	   var pathname = url.parse(request.url).pathname;//获取路径名称
      pathname = pathname.replace(/\//,""); //正则去掉/
      console.log(pathname);
      router[pathname](request,response);//根据路径名称获取到函数从而调用函数
  }
}).listen(8000);  
console.log('Server  running  at  http://127.0.0.1:8000/');  

  router.js

var openfile = require('./openfile');
module.exports={
    login:function(req,res){
         function recall(date){
 	 	res.write(date);
 	 	res.end('ok');
 	 	
 	 }
 	 openfile.readfile('./view/login.html',recall);
     
    },
    zhuce:function(req,res){
        function recall(date){
 	 	res.write(date);
 	 	res.end('ok');	
 	 }
 	 openfile.readfile('./view/zhuce.html',recall);
     
    }
}

  openfile.js

var  fs=  require('fs');//node自带的类
module.exports={
    readfile:function(path,recall){          //异步执行
        fs.readFile(path,  function  (err,  data)  {
            if  (err)  {
              console.log(err);
            }else{
              console.log(data.toString());
              recall(data);
            }
        });
        console.log("异步方法执行完毕");
    }
    
}

  请求路径

posted @ 2017-12-12 23:43  款款就是我  阅读(175)  评论(0编辑  收藏  举报