Read a file in Node.js

后端 未结 8 1141
礼貌的吻别
礼貌的吻别 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)
    

提交回复
热议问题