Nodejs学习笔记-文件读取

同步读文件

代码:https://github.com/fengchunjian/nodejs_examples/tree/master/readfile

//vim views/login.html
登录界面
//vim models/optfile.js
var fs = require('fs')
module.exports = {
    readfileSync : function(path, res) {
        var data = fs.readFileSync(path, "utf-8");
        console.log(data);
        console.log("同步方法执行完毕");
        res.write(data);
    }
}
\\vim readfile.js
var http = require('http');
var optfile = require("./models/optfile");
http.createServer(function (request, response) {
    optfile.readfileSync("./views/login.html", response);
    console.log("主程序执行完毕");
    response.end();
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');

node readfile.js
Server running at http://127.0.0.1:8000/
登录界面
同步方法执行完毕
主程序执行完毕

curl http://127.0.0.1:8000/
登录界面

异步读文件+路由升级

代码:https://github.com/fengchunjian/nodejs_examples/tree/master/routerv2

//vim views/login.html
登录界面
//vim views/zhuce.html
注册界面
\\vim models/optfile.js
var fs = require('fs')
module.exports = {
    readfile : function(path, recall) {
        fs.readFile(path, function(err, data) {
            if (err) {
                console.log(err);
            } else {
                recall(data);
            }
        });
        console.log("异步方法执行完毕");
    },
    readfileSync : function(path, res) {
        var data = fs.readFileSync(path, "utf-8");
        console.log(data);
        console.log("同步方法执行完毕");
        res.write(data);
    }
}
\\vim models/router.js
var optfile = require("./optfile");
module.exports = {
    login : function(req, res) {
        function recall(data) {
            res.write(data);
            res.end();
        }
        optfile.readfile("./views/login.html", recall);
    },
    zhuce : function(req, res) {
        function recall(data) {
            res.write(data);
            res.end();
        }
        optfile.readfile("./views/zhuce.html", recall);
    }
}
\\vim routercall.js
var http = require('http');
var url = require('url');
var router = require('./models/router');
http.createServer(function (request, response) {
    var pathname = url.parse(request.url).pathname;
    pathname = pathname.replace(/\//, '');
    router[pathname](request, response);
    console.log("主程序执行完毕");
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');

node routercall.js
Server running at http://127.0.0.1:8000/
异步方法执行完毕
主程序执行完毕
异步方法执行完毕
主程序执行完毕

curl http://127.0.0.1:8000/login
登录界面
curl http://127.0.0.1:8000/zhuce
注册界面

参考文档

node.js教程5_读文件
http://edu.51cto.com/center/course/lesson/index?id=124529
nodejs5_读取文件(n5_readfile)
http://www.yuankuwang.com/web/index.php?r=respool/resview&rpid=37

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,767评论 19 139
  • 个人入门学习用笔记、不过多作为参考依据。如有错误欢迎斧正 目录 简书好像不支持锚点、复制搜索(反正也是写给我自己看...
    kirito_song阅读 7,210评论 1 37
  • Node基本 node的最大特性莫过于基于事件驱动的非阻塞I/O模型。 node通过事件驱动的方式处理请求,无须为...
    AkaTBS阅读 6,625评论 0 11
  • Node.js是目前非常火热的技术,但是它的诞生经历却很奇特。 众所周知,在Netscape设计出JavaScri...
    w_zhuan阅读 8,983评论 2 41
  • 使用 HTTP 服务器或客户端功能必须调用require('http')。 Node 里的 HTTP 接口支持协议...
    保川阅读 5,215评论 0 1