温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

node.js学习笔记之读文件

发布时间:2020-08-14 01:02:21 来源:网络 阅读:316 作者:pkutao 栏目:web开发

 
直接读硬盘上aa.txt内容
var  http  =  require('http');
var  optfile  =  require('./models/optfile');
http.createServer(function  (request,  response)  {
    response.writeHead(200,  {'Content-Type':  'textnode.js学习笔记之读文件ml;  charset=utf-8'});
    if(request.url!=="/favicon.ico"){  //清除第2此访问
        console.log('访问');
        response.write('hello,world');
        //optfile.readfile("G:node.js学习笔记之读文件\\www\\nodejs\\one\\models\\aa.txt");
        optfile.readfileSync("G:node.js学习笔记之读文件\\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;                
    }
}

教程学习地址:
node.js学习笔记之读文件http://study.163.com/course/introduction/1003228034.htm#/courseDetail

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI