Read a file in Node.js

后端 未结 8 1085
礼貌的吻别
礼貌的吻别 2020-11-27 10:22

I\'m quite puzzled with reading files in Node.js.

fs.open(\'./start.html\', \'r\', function(err, fileToRead){
    if (!err){
        fs.readFile(fileToRead,         


        
相关标签:
8条回答
  • 2020-11-27 11:12

    simple synchronous way with node:

    let fs = require('fs')
    
    let filename = "your-file.something"
    
    let content = fs.readFileSync(process.cwd() + "/" + filename).toString()
    
    console.log(content)
    
    0 讨论(0)
  • 2020-11-27 11:15

    1).For ASync :

    var fs = require('fs');
    fs.readFile(process.cwd()+"\\text.txt", function(err,data)
                {
                    if(err)
                        console.log(err)
                    else
                        console.log(data.toString());
                });
    

    2).For Sync :

    var fs = require('fs');
    var path = process.cwd();
    var buffer = fs.readFileSync(path + "\\text.txt");
    console.log(buffer.toString());
    
    0 讨论(0)
提交回复
热议问题