Read a file in Node.js

后端 未结 8 1140
礼貌的吻别
礼貌的吻别 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 10:56

    Run this code, it will fetch data from file and display in console

    function fileread(filename)
    {            
       var contents= fs.readFileSync(filename);
       return contents;
    }        
    var fs =require("fs");  // file system        
    var data= fileread("abc.txt");
    //module.exports.say =say;
    //data.say();
    console.log(data.toString());
    

提交回复
热议问题