代码之家  ›  专栏  ›  技术社区  ›  user944513

如何读取文件内容并将文件存储在目录中?

  •  1
  • user944513  · 技术社区  · 5 年前

    read 一 css 文件来自 url nodejs .

    https://repl.it/@naveennsit/ImpeccableQuarrelsomeDebugging

    我是这样读的

    const fs = require('fs');
    
    
        fs.readFileSync('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', 'utf8', function (err,data) {
          if (err) {
            return console.log(err);
          }
          console.log(data);
        });
    

    css格式

    目前我得到这个错误

    ror: ENOENT: no such file or directory, open 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'
        at Object.openSync (fs.js:462:3)
        at Object.readFileSync (fs.js:364:35)
        at /home/runner/ImpeccableQuarrelsomeDebugging/index.js:4:4
        at Script.runInContext (vm.js:130:18)
        at Ob
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   Zac    5 年前

    FS模块只能读取本地文件,请尝试向发送GET请求 https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js 然后将响应体写入css目录

    举个例子:

    const axios = require('axios');
    axios.get('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js')
      .then(function (response) {
       fs.writeFile('css.txt', response, () => {
       console.log('The file has been saved!');
      })
    })