简体   繁体   English

在响应快递中呈现带有文件的页面

[英]render page with files in response express

want to send files from server to be rendered in browser in index pug想要从服务器发送文件以在索引 pug 的浏览器中呈现

   extends layout block content h1= 'saved files' #recordings.recordings.row script(src='/javascripts/listrecordings.js')
    function fetchRecordings() {
      fetch('/')
        .then((response) => response.json())
        .then((response) => {
          console.log("response " + response);
          if (response.success && response.files) {
            //remove all previous recordings shown
            recordingsContainer.innerHTML = '';
            response.files.forEach((file) => {
              //create the recording element
              const recordingElement = createRecordingElement(file);
              //add it the the recordings container
              recordingsContainer.appendChild(recordingElement);
            })
          }
        })
        .catch((err) => console.error(err));
    };

And the part of index js:和索引js的部分:

    router.get('/', (req, res) => {
      let files = fs.readdirSync('./public/upploads');
      console.log(__dirname + " files length  " + files.length);
      files = files.filter((file) => {
        // check that the files are audio files
        const fileNameArr = file.split('.');
        return fileNameArr[fileNameArr.length - 1] === 'mp3';
      }).map((file) => `/${file}`);
      //res.json({ success: true, files });
      res.render('index', files);// stuck here ???

    });

how to do this part to shoot response to index如何做这部分拍摄对索引的反应

I think a better way to do it is to return an array of paths of the .mp3 files for example:我认为更好的方法是返回.mp3文件的路径数组,例如:

['upploads/a.mp3', 'upploads/b.mp3' ]

And in the client just need to load the audio using the url:而在客户端只需要使用 url 加载音频:

http://localhost:3000/upploads/a.mp3 http://localhost:3000/upploads/a.mp3

To setup your public directory read this docs .要设置您的公共目录,请阅读此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM