• xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!


    node.js & fs & file read & file write

    https://nodejs.org/api/fs.html

    
    const fs = require("fs");
    
    
    // absolute path
    fs.open("/open/some/file.txt", "r", (err, fd) => {
        if (err) {
            throw err;
        }
        fs.close(fd, (err) => {
            if (err) {
                throw err;
            }
        });
    });
    
    // relative path
    fs.open("file.txt", "r", (err, fd) => {
        if (err) {
            throw err;
        }
        fs.close(fd, (err) => {
            if (err) {
                throw err;
            }
        });
    });
    
    
    

    readline

    https://nodejs.org/api/readline.html

    
    const readline = require('readline');
    
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    
    rl.question('What do you think of Node.js? ', (answer) => {
      // TODO: Log the answer in a database
      console.log(`Thank you for your valuable feedback: ${answer}`);
    
      rl.close();
    });
    
    

    
    const fs = require("fs");
    const readline = require('readline');
    
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });
    
    // absolute path
    // fs.open("/open/some/file.txt", "r", (err, fd) => {
    //     if (err) {
    //         throw err;
    //     }
    //     fs.close(fd, (err) => {
    //         if (err) {
    //             throw err;
    //         }
    //     });
    // });
    
    // relative path
    fs.open("file.txt", "r", (err, fd) => {
        if (err) {
            throw err;
        }
        console.log(`fd =`, fd);// 3
        rl.on('line', (fd) => {
        console.log(`Received: ${fd}`);
        });
        fs.close(fd, (err) => {
            if (err) {
                throw err;
            }
        });
    });
    
    
    
    

    bug

    https://stackoverflow.com/a/15554600/5934465


    write & read

    https://stackoverflow.com/a/15554600/5934465
    https://stackoverflow.com/a/2497040/5934465

    // preview.js
    
    const fs = require("fs");
    const readline = require("readline");
    
    fs.writeFile("file.js", `export const APP_ENV = "preview";`, function(err) {
        if(err) {
            console.log(err);
            return err;
        }
        let env = "preview";
        console.log("The file was saved!", env);
    });
    
    let rd = readline.createInterface({
        input: fs.createReadStream("file.js"),
        output: process.stdout,
        console: false
    });
    
    rd.on("line", function(line) {
        console.log(`line =`, line);
    });
    
    
    
    
    // fs.writeFile("file.txt", "prview", function(err) {
    //     if(err) {
    //         console.log(err);
    //         return err;
    //     }
    //     console.log("The file was saved!");
    // });
    
    // global.APP_ENV = "testing";
    // let APP_ENV = global.APP_ENV;
    // console.log(`APP_ENV =`, APP_ENV);
    
    // let rd = readline.createInterface({
    //     input: fs.createReadStream('file.txt'),
    //     output: process.stdout,
    //     console: false
    // });
    
    // rd.on('line', function(line) {
    //     console.log(`line =`, line);
    // });
    
    
    
    
    
    // testing.js
    
    const fs = require("fs");
    const readline = require("readline");
    
    
    fs.writeFile("file.js", `export const APP_ENV = "testing";`, function(err) {
        if(err) {
            console.log(err);
            return err;
        }
        let env = "testing";
        console.log("The file was saved!", env);
    });
    
    // const rl = readline.createInterface({
    //     input: process.stdin,
    //     output: process.stdout
    // });
    
    // absolute path
    // fs.open("/open/some/file.txt", "r", (err, fd) => {
    //     if (err) {
    //         throw err;
    //     }
    //     fs.close(fd, (err) => {
    //         if (err) {
    //             throw err;
    //         }
    //     });
    // });
    
    
    // global.APP_ENV = "testing";
    // let APP_ENV = global.APP_ENV;
    // console.log(`APP_ENV =`, APP_ENV);
    
    // relative path
    // fs.open("file.js", "r", (err, fd) => {
    //     if (err) {
    //         throw err;
    //     }
    //     console.log(`fd =`, fd);// 3
    //     fs.close(fd, (err) => {
    //         if (err) {
    //             throw err;
    //         }
    //     });
    // });
    
    
    let rd = readline.createInterface({
        input: fs.createReadStream("file.js"),
        output: process.stdout,
        console: false
    });
    
    rd.on("line", function(line) {
        console.log(`line =`, line);
    });
    
    
    
    
    
    // file.js
    
    export const APP_ENV = "testing";
    
    

    global var

    https://stackabuse.com/using-global-variables-in-node-js


  • 相关阅读:
    遇见Javascript类型数组
    编译Android常用命令
    V4L2驱动视频开发要点
    Windows Phone开发(27):隔离存储A
    Ubuntu10.04下Android开发环境搭建
    V4L2开发要点
    使用 php Header 报错的一个原因
    Windows Phone开发(28):隔离存储B
    用HTML5 Audio API开发游戏音乐
    php备份数据库类分享
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/10827874.html
Copyright © 2020-2023  润新知