Nodejs学习笔记-写文件

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

//vim models/optfile.js
var fs = require('fs')
module.exports = {
    writefile : function(path, data, recall) {
        fs.writeFile(path, data, function(err) {
            if (err) {
                throw err;
            }
            console.log("异步写文件完成");
            recall("异步写文件完成");
        });
    },
    writefileSync : function(path, data, res) {
        fs.writeFileSync(path, data);
        console.log("同步写文件完成");
        res.write("同步写文件完成");
    },
    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 = {
    writefile : function(req, res) {
        function recall(data) {
            res.write(data);
            res.end();
        }
        optfile.writefile("./file.txt", "异步文件写入", recall);
    },
    writefileSync : function(req, res) {
        optfile.writefileSync("./sync.txt", "同步文件写入", res);
        res.end();
    },
    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://localhost:8000/writefileSync
同步写文件完成
curl http://localhost:8000/writefile
异步写文件完成

cat sync.txt
同步文件写入
cat file.txt
异步文件写入

参考文档

nodejs6_写文件(n6_write)
http://www.yuankuwang.com/web/index.php?r=respool/resview&rpid=38
node.js教程6_写文件
http://edu.51cto.com/center/course/lesson/index?id=124530

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

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,967评论 19 139
  • 太长了,还是转载吧...今天在看博客的时候,无意中发现了@Trinea在GitHub上的一个项目Android开源...
    庞哈哈哈12138阅读 20,274评论 3 283
  • 刚结婚那会儿,很烦恼。也许是期待过高,也许是激情过后太过平淡!总之,有时候甚至感觉后悔。结婚这事儿,本来就不...
    靠谱同志小贾阅读 273评论 0 0
  • 最近在简上面看到两篇叫人们不要读鸡汤文的文章,不知道这算不算又一种形式的鸡汤。很多东西一直不知道如何区分,什么是好...
    好迷茫阅读 116评论 0 2