运行函数多次返回“错误:发送后无法设置标题”

问题描述 投票:0回答:1

程序按原样运行,直到我再次尝试使用该函数并返回错误:发送后无法设置标头。

我正在创建这个应用程序以熟悉nodejs和javascript,我一直在阅读有关错误的信息,并且在向请求发送多个响应时似乎是一个问题。在开始知道这个之前我开始使用res.setHeader,但我读到res.header可以避免这个问题,它没有解决它,但我保留了它。

HTML:

<!doctype html>
<head>
    <link rel="stylesheet" type="text/css" href="youtube2music.css">
    <title>
        Youtube2Music
    </title>

</head>
<body>
    <div id="cabeza">
        <h1>
            Youtube 2 Music
        </h1>
        <p>
            just paste your link below and download your song.
        </p>
    </div>
    <div id="down-part">
        <input id="myUrl" class='myUrl-input'>
        </input>
        <button type="button" class="download_button">
        Download
        </button>
    </div>
</body>
<script src='youtube2music.js'></script>
</html>

使用Javascript:

var urlinput = document.querySelector('.myUrl-input'); // gets url inputbox
var button = document.querySelector('.download_button'); // gets download button

button.addEventListener('click', () => {

  console.log(urlinput.value); // prints in console the url
  sendUrl(urlinput.value); // sends url to function to start the request

});

// function to make requst
function sendUrl(URL){

  window.location.href = `http://localhost:4000/?URL=${URL}`; // makes the video request to nodejs server
}

index.js <node file:

var eventEmitter = new events.EventEmitter();
var sfn;
appi.use(cors());
const {app, BrowserWindow} = require('electron')

function createWindow(){
  let win = new BrowserWindow({width:800, height:600});
  win.loadFile('index.html');
}
app.on('ready', createWindow)

appi.listen(4000, () => {
 console.log('server at port 4000');
});

appi.get('/',(req,res)=>{
 var URL = req.query.URL;
 ytdl.getInfo(URL, function(err,info){
   if(err) throw err;
   var songTitle = info.title;
   sfn = filenamify(songTitle);
   eventEmitter.emit('name_ready');
 });

 var startDownload = function(){
  let stream = ytdl(URL, {
   quality: 'highestaudio',
  });
  res.header('Content-Disposition', 'attachment; filename=' + sfn + '.mp3');
  res.header('Content-type', 'audio/mpeg');
  proc = new ffmpeg({source: stream})
  proc.withAudioCodec('libmp3lame').toFormat('mp3').output(res).run();
 }
 eventEmitter.on('name_ready', startDownload);
})

因为它适用于第一个输入,但要求另一个输出导致错误,为什么它真的返回此错误,如何避免?

javascript node.js express http-headers electron
1个回答
1
投票

您当前的设置存在几个问题:

  1. 尽量不要在HTTP请求中使用事件发射器来发送信号事件,但这并不是为此而做的。
  2. 对于HTTP请求,尽量不要对请求期间收到的数据使用全局变量,当两个请求同时进入时,它们可能会混淆并被发送错误的数据。
appi.listen(4000, () => {
 console.log('server at port 4000');
});

appi.get('/', (req,res)=> {
  const { URL } = req.query;
  ytdl.getInfo(URL, (err,info) => {
    if(err) throw err;
    const songTitle = info.title;
    const sfn = filenamify(songTitle);
    let stream = ytdl(URL, {
      quality: 'highestaudio',
    });
    res.set('Content-Disposition', 'attachment; filename=' + sfn + '.mp3');
    res.set('Content-type', 'audio/mpeg');
    const proc = new ffmpeg({source: stream})
    proc.withAudioCodec('libmp3lame').toFormat('mp3').output(res).run();
  });
})
© www.soinside.com 2019 - 2024. All rights reserved.