nodejs文件的读取

读取:

read.js

//导入http
var http = require('http');
var optfile = require('../module/optfile.js');
//创建
http.createServer(function (request,response) {
    response.writeHead(200,{'Content-type':'text/html;charset=utf-8'});
    if(request.url !== '/favicon.ico'){//清除二次访问
        console.log('访问');
        //闭包,回调这个函数,客户端打印程序
        function recall(data){
            response.write(data);
            response.end("");
        }
        // optfile.readfileSync('../view/login.html');
        optfile.readfile('../view/login.html',recall)
        // response.end("世界");//不写会没有协议尾部,但是写了会访问俩次
        console.log('主程序执行完毕');
    }
}).listen(8001);
console.log('Server running at http://127.0.0.11:8001/')

module/optfile.js

var fs=require('fs');
module.exports={
    readfileSync:function(path){//同步读取
        var data = fs.readFileSync(path,'utf-8');
        console.log('同步方法执行完毕');
        // return data;
    },
    readfile:function(path,recall){ //异步执行
        fs.readFile(path,function(err,data){
            if(err){
                console.log(err);
            }else{
                console.log(data.toString());
                recall(data);
            }
        })
    },
}

 

 利用路由,传入不同的页面读取不同的数据:

router.js

var optfile = require('../module/optfile.js');
module.exports={
    login:function(req,res){
        //闭包,回调这个函数,客户端打印程序
        function recall(data){
            res.write(data);
            res.end("");
        }
        optfile.readfile('../view/login.html',recall)
    },
    zhuce:function(req,res){
        //闭包,回调这个函数,客户端打印程序
        function recall(data){
            res.write(data);
            res.end("");
        }
        optfile.readfile('../view/zhuce.html',recall)
    },
}

readfile.js

//导入http
var http = require('http');
// var optfile = require('../module/optfile.js');
var url=require('url');
var router = require('../module/router.js');
//创建
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(8021);
console.log('Server running at http://127.0.0.11:8021/');

optfile.js

var fs=require('fs');
module.exports={
    readfileSync:function(path){//同步读取
        var data = fs.readFileSync(path,'utf-8');
        console.log('同步方法执行完毕');
        // return data;
    },
    readfile:function(path,recall){ //异步执行
        fs.readFile(path,function(err,data){
            if(err){
                console.log(err);
            }else{
                console.log(data.toString());
                recall(data);
            }
        })
    },
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值