美文网首页
Nodejs学习笔记-写文件

Nodejs学习笔记-写文件

作者: 莫名FCJ | 来源:发表于2017-10-24 10:49 被阅读31次

    代码: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

    相关文章

      网友评论

          本文标题:Nodejs学习笔记-写文件

          本文链接:https://www.haomeiwen.com/subject/yljtpxtx.html