Javascript 多次运行函数返回“错误:发送邮件后无法设置邮件头”

Javascript 多次运行函数返回“错误:发送邮件后无法设置邮件头”,javascript,node.js,express,http-headers,electron,Javascript,Node.js,Express,Http Headers,Electron,程序正常运行,直到我再次尝试使用该函数,它返回错误:发送头后无法设置头 我创建这个应用程序是为了熟悉nodejs和javascript,我一直在阅读有关错误的信息,当发送多个请求响应时,这似乎是一个问题。我在知道这一点之前就开始使用res.setHeader,但我读到res.header可以避免这个问题,它没有解决问题,但我保留了它 HTML: index.js

程序正常运行,直到我再次尝试使用该函数,它返回错误:发送头后无法设置头

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

HTML:

index.js<节点文件:

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);
})

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

当前设置存在几个问题:

尽量不要在HTTP请求中使用事件发射器来发送事件信号,它不是为此而设计的。 对于HTTP请求,尽量不要对请求期间接收到的数据使用全局变量,当两个请求同时进入时,它们可能会混淆并发送错误的数据。 我没有想到使用const谢谢,你能解释一下为什么在这种情况下使用res.set吗?res.set是res.header的文档版本:
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
}
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);
})
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();
  });
})