Get data from fs.readFile

前端 未结 15 2540
滥情空心
滥情空心 2020-11-22 15:38
var content;
fs.readFile(\'./Index.html\', function read(err, data) {
    if (err) {
        throw err;
    }
    content = data;
});
console.log(content);
         


        
15条回答
  •  我在风中等你
    2020-11-22 16:11

    sync and async file reading way:

    //fs module to read file in sync and async way
    
    var fs = require('fs'),
        filePath = './sample_files/sample_css.css';
    
    // this for async way
    /*fs.readFile(filePath, 'utf8', function (err, data) {
        if (err) throw err;
        console.log(data);
    });*/
    
    //this is sync way
    var css = fs.readFileSync(filePath, 'utf8');
    console.log(css);
    

    Node Cheat Available at read_file.

提交回复
热议问题