Node.js 使用发射器/侦听器处理多个POST请求

Node.js 使用发射器/侦听器处理多个POST请求,node.js,express,listener,eventemitter,Node.js,Express,Listener,Eventemitter,代码: app.post('/new', urlencodedParser, function (req, res) { if (!req.body) return res.sendStatus(400); emitter.on('response', function (output) { res.status(200).json({ip: `${output}`, id: `${random_id}`}); }); }); 问题: app.post('/new', u

代码:

app.post('/new', urlencodedParser, function (req, res) {
  if (!req.body) return res.sendStatus(400);

  emitter.on('response', function (output) {
    res.status(200).json({ip: `${output}`, id: `${random_id}`});
  });
});
问题:

app.post('/new', urlencodedParser, function (req, res) {
  if (!req.body) return res.sendStatus(400);

  emitter.on('response', function (output) {
    res.status(200).json({ip: `${output}`, id: `${random_id}`});
  });
});
我现在遇到的问题(我使用的是
express
)是我无法向应用程序发出多个请求。Express第一次返回预期值,之后每次都返回:

Error[ERR\u HTTP\u HEADERS\u SENT]:发送到客户端后无法设置头

应用程序的其余部分是由事件发射器和侦听器触发的函数集合。将其视为一个发射器字符串,触发侦听器并连续执行4或5个函数,最后返回一个值

这是第一次完美地工作,但再也不会了。请注意,请求预期来自不同的机器,而不是单个机器

我知道
res.end()
将结束响应,但Express函数的组合不会使其第二次工作。有没有一种方法可以使用发射器/侦听器并表达响应

提前谢谢

编辑:更多代码

emitter.on('newRequest', persistentDisk);
emitter.on('newDisk', persistentDisk);
emitter.on('newDeployment', newDeployment);
emitter.on('newService', createService);
emitter.on('checkDeployment', checkDeployment);
emitter.on('returnIp', returnIp);

emitter.on('deleteRequest', deleteApp);

app.post('/new', urlencodedParser, function (req, res) {
  if (!req.body) return res.sendStatus(400);

  var random_id = randomize('a', 8);

  var user_id = req.body.user_id;
  var stripe_id = req.body.stripe_id;
  var app_name = req.body.app_name;
  var gpu_count = req.body.gpu_count;
  var cpu_count = req.body.cpu_count;
  var memory_count = req.body.memory_count;

  emitter.emit('newRequest', random_id, user_id, stripe_id, app_name, gpu_count, cpu_count, memory_count);
  emitter.on('response', function (output) {
    res.send({ip: `${output}`, id: `${random_id}`});
  });
    async function persistentDisk(random_id, user_id, stripe_id, app_name, gpu_count, cpu_count, memory_count) {
      try {
            emitter.emit('newDeployment', random_id, user_id, stripe_id, app_name, gpu_count, cpu_count, memory_count);
            emitter.emit('newService', random_id, user_id, stripe_id, app_name, gpu_count, cpu_count, memory_count);
      } catch (err) {
        console.log(err);
        errors.report(err);
      }
    }

async function newDeployment(random_id, user_id, stripe_id, app_name, gpu_count, cpu_count, memory_count) {
  try {
    var deployConfig = JSON.parse(`...`);

      emitter.emit('checkDeployment', random_id, user_id, stripe_id, app_name, gpu_count, cpu_count, memory_count);

  } catch (err) {
    console.log(err);
    errors.report(err);
  }
}

async function createService(random_id, user_id, stripe_id, app_name, gpu_count, cpu_count, memory_count) {
  try {
    var exposeConfig = JSON.parse(`...`);

      emitter.emit('returnIp', random_id, user_id, stripe_id, app_name, gpu_count, cpu_count, memory_count)
  } catch (err) {
    console.log(err);
    errors.report(err);
  }
}

async function checkDeployment(random_id, user_id, stripe_id, app_name, gpu_count, cpu_count, memory_count) {
  try {
  } catch (err) {
    console.log(err);
    errors.report(err);
  }
}

async function returnIp(random_id, user_id, stripe_id, app_name, gpu_count, cpu_count, memory_count) {
  try {
    var service = JSON.parse(`{"ip":"127.0.0.1"}`)
    jq.run('.ip', service, { input: 'json' }).then((output) => {
      emitter.emit('response', output)
      if (output.toString() === "null") {
        setInterval(() => {
          console.log('value is null');
          emitter.emit('returnIp', random_id, user_id, stripe_id, app_name, gpu_count, cpu_count, memory_count)
        }, 15000);
        }
      });
  } catch (err) {
    console.log(err);
    errors.report(err);
  }
}
这将第一次生成响应(
127.0.0.1
,重要的是)。

更改为:

app.post('/new', urlencodedParser, function (req, res) {
  if (!req.body) return res.sendStatus(400);

  emitter.once('response', function (output) {
    res.status(200).json({ip: `${output}`, id: `${random_id}`});
  });
});

(从emitter.on到emitter.one)解决了问题。

您不能两次结束请求,您想实现什么?如果需要向客户端发送数据,则应使用WebSocket。这些函数字符串中的最后一个函数具有调用
emitter.emit('response',output)
的If-else语句。编辑:@MarcosCasagrande我正在尝试实现一个应用程序,该应用程序可以使用发射器触发一系列函数的计算值来响应来自不同机器的多个请求。如果
emitter,则显示您的代码。emit
对于请求不是唯一的,这就是您代码中发生的情况,“你会遇到这个问题的。”MarcosCasagrande用更多的代码编辑。如何使
emitter.emit
“对于请求来说是唯一的”?要么为每个请求创建一个事件发射器,因为该事件与请求没有关联,对您没有帮助,要么使用WebSocket,这是您应该使用的,在可能永远不会发生的事件上结束请求将导致超时问题。它将“解决”问题在于,这可能会导致超时,或发布不同的IP地址,因为不同的请求可能会发出
响应
事件。看来你对我向你解释的所有问题都不感兴趣。你处理这个问题的方法是错误的。