当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Node.js入门教程之简单读写文件

Node.js入门教程之简单读写文件

2017年12月18日  | 移动技术网IT编程  | 我要评论
Node.js入门教程之简单读写文件

本篇小编主要介绍node.js读取html,并且想html文件写入的方式。

内容

目录结构 file文件下存放了login.html,module文件下存放optfile.js
这里写图片描述

1.optfile.js

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

    },
    readfile:function(path){ //异步执行
        fs.readFile(path,function(err,data){
            if(err){
                console.log(err);
            }else{
                console.log(data.toString());
            }
        });
        console.log("异步方法执行完毕");
    }
}

2.主文件JS

var http=require('http');
var optfile=require('./module/optfiles');
http.createServer(function(request,response){
    response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
    if(request.url!="/favicon.ico"){//清除第二次访问
        optfile.readfileSync('./file/login.html');
        response.end('ok');
        console.log('主程序结束');
    }
}).listen(8000);

console.log("这个路由");

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网