zoukankan      html  css  js  c++  java
  • node基础07:写文件

    1.writeFile

    //server.js
    
    var http = require("http");
    var writefile = require("./writefile");
    
    http.createServer(function(res, res){
        res.writeHead(200, {"Content-Type":"text/html; charset=uf-8"});
        if (res.url!=="/favicon.ico") {
            console.log('begin visit');
            res.write('hello world');
            writefile.writefile('./test.txt', 'gaoxiong'); //如果文件路径存在则添加数据,如果不存在则新建文件并且添加数据
            res.end('end');
        }
    }).listen(8000);
    console.log('Server running at http://127.0.0.1:8000/');
    //writefile.js
    var fs = require("fs");
    module.exports = {
        //异步方式
        writefile:function(path, data){
            fs.writeFile(path, data, function(err){
                if (err) {
                    throw(err)
                } else{
                    console.log('saved success');
                }
            })
        },
        //同步方式
        writefileSync:function(path, data){
            fs.writeFileSync(path, data);
            console.log('同步写文件完成')
        }
    }
  • 相关阅读:
    WCF上传下载文件
    WCF使用相关
    .net WCF WF4.5 状态机、书签与持久化
    .net WCF WF4.5
    CSS小东西
    asp.net mvc导出execl_转载
    winform自定义控件开发
    html问题汇总
    工作中的小东西
    jQuery事件
  • 原文地址:https://www.cnblogs.com/noper/p/6244589.html
Copyright © 2011-2022 走看看