读取:

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);}})},
}

nodejs文件的读取相关推荐

  1. nodejs fs创建文件_节点FS – NodeJS创建文件,读取文件,写入文件

    nodejs fs创建文件 Node FS stands for NodeJS File System module. In my previous post, we have already dis ...

  2. Nodejs 文件 与 路径 相关用法实例解析

    Nodejs 文件 与 路径 相关用法 CSDN用户名:jcLee95 邮箱:291148484@163.com 原创不易,感谢点赞支持. 1.路径 (1)引入path模块 const path = ...

  3. NodeJS 文件操作 —— fs 基本使用

    阅读原文 fs 概述 在 NodeJS 中,所有与文件操作都是通过 fs 核心模块来实现的,包括文件目录的创建.删除.查询以及文件的读取和写入,在 fs 模块中,所有的方法都分为同步和异步两种实现,具 ...

  4. 2021-10-16 NodeJS文件模块面试题

    1.简述git中常见的操作命令以及它们的含义 2.简述git和svn的区别,如何在项目中选择使用什么版本工具呢? git是第三代版本管理工具,核心是以分布式的方式.差异化的备份进行文件数据的版本迭代管 ...

  5. 向HBase中导入数据3:使用MapReduce从HDFS或本地文件中读取数据并写入HBase(增加使用Reduce批量插入)

    前面我们介绍了: 向HBase中导入数据1:查询Hive后写入 向HBase中导入数据2:使用MapReduce从HDFS或本地文件中读取数据并写入HBase(只使用Map逐条查询) 为了提高插入效率 ...

  6. vc++从txt文件中读取数据

    数值分析课上老师说要将数据写在txt文件上,然后让程序从txt文件中读取数据.让本来C++已经遗忘了很久的我们无从下手,在网上也查看了很多,发现大多都是扯淡,放在VC++编辑器上发现并不能运行,不知道 ...

  7. android中XMl文件的读取

    废话不多说了直接上代码: 新建一个Xml文件用于读取: test.xml <Languages cat="1"><lan id="1"> ...

  8. php 操作txt,PHP 从txt文件中读取数据

    一.打开/关闭文件 1.对文件操作时首先要打开文件,打开文件用 fopen()函数,语法是: fopen(filename,mode,include_path,context); 2.对文件操作结束后 ...

  9. python中如何在写文件之前删除文件内容_Python:文件的读取、创建、追加、删除、清空...

    一.用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行: #python >>>f=open('f.txt','w') # r只读,w可写,a追加 >> ...

最新文章

  1. h5滚动时侧滑出现_H5触摸事件中如何判断用户滑动方向
  2. MFC中小笔记(三)
  3. 浅谈Linux中ldconfig和ldd的用法
  4. python变量如何使用,python如何使用变量
  5. 支持多达70余种嵌入式核心的嵌入式仿真软件SkyEye
  6. RewriteCond 详解
  7. Spring : Spring Aop JDK和CGLIB动态代理调用过程
  8. LINUX 学习笔记 账号与群组的管理
  9. 记一次route配置不起作用的问题解决过程
  10. excel 使用定位工具批量删除错误值
  11. FreeBSD下使用Blogbio写cnblogs博客
  12. 《Python简明教程》一
  13. 打开chm文件c语言,chm格式,手把手教你chm文件怎么打开
  14. oracle18c 配置,0报错!Oracle 18C 完全安装指南及常见问题汇总
  15. ROS 入门基础(三)创建 Subscriber
  16. Qt For Android 屏幕常亮
  17. 工作十年,我是如何达到年薪40W以上
  18. Python 大作业
  19. 索引合并和组合索引的比较
  20. 如何优雅的用Axure绘制高保真原型心得分享

热门文章

  1. [Bootstrap]bootstrap的简单原理
  2. Django 的系统时区设置 RPC
  3. JAVA遍历map元素
  4. 基于MVC模式Struts框架研究
  5. jquery实现的滚动新闻(多个实例代码)
  6. 马云在《赢在中国》对创业者的经典点评
  7. 郭新华 php,PHP常驻进程编程需知 郭新华 PHPCON2019
  8. 如何将两部手机内容互换_两部iPhone手机之间如何大量互传照片?
  9. 设计师交流分享社区|灵感并非凭空得来,积累在集设网
  10. 流行趋势-立体感和艺术剪纸风海报美妆设计