Node.js入门(二)——简单读写文件
来源:互联网 发布:工商名录采集软件 编辑:程序博客网 时间:2024/06/03 21:39
前言
本篇小编主要介绍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("这个路由");
结语
感谢大家浏览,希望带给你帮助!
阅读全文
0 0
- Node.js入门(二)——简单读写文件
- Node.js 读写文件
- node.js 读写文件
- Node.js读写文件
- Node.js fs模块——读写文件
- 从零开始node.js入门项目(二)
- Node.js开发入门—Angular简单示例
- Node.js开发入门(二)——HelloWorld再分析
- Node.js 入门之二
- Node.js-入门二--进程
- Node.js开发入门(八)——Angular简单示例
- nodejs简单应用二(读写文件)
- 转载:Node.js入门系列——《深入浅出Node.js》
- Node.js入门——mac下安装Node.js
- Node.js开发入门—notepad++ for Node.js
- node.js--jade,路由,文件的读写
- Node.js读写中文内容文件操作
- Node.js读写中文内容文件操作
- 3Sum Closest -- LeetCode
- 使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- ncnn编译使用(一)
- 元组有关
- 高性能网络编程(七)--tcp连接的内存使用
- Node.js入门(二)——简单读写文件
- least-2017
- 【BZOJ2242】计算器
- VIM系列一:ctags安装使用说明
- 学习笔记
- 优先级队列.c
- 进程间通信(IPC)1 ------ 管道
- linux下增加一个新的系统调用实现pstree功能
- 上传代码到github时报错Please tell me who you are