Search code examples
node.jsexpresseventsresponseeventemitter

Can't set headers after they are sent when using EventEmitter


Here is my code:

const cluster = require('cluster');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const queueMgr = require('./lib/queueManager');
const eventEmitter = new (require('events')).EventEmitter();
  const app = express();

  app.use(cors());

  app.use((req, res, next) => {
    if (channel) {
      next();
    }
    queueMgr
      .connect()
      .then(ch => {
        channel = ch;
        next();
      })
      .catch(err => {
        console.log(err);
        res
          .status(500)
          .json({ message: 'Something Went Wrong' })
          .end();
      });
  });

  app.get('/', (req, res) => {
    const taskId = Date.now() + '' + process.hrtime()[1];
    const worker = queueMgr.launchWorker();
    eventEmitter.once(taskId, msg => {
      console.log(taskId);
      res
        .status(200)
        .send({ message: msg, status: true })
        .end();
    });
    queueMgr.addTaskToQueue(channel, config.taskQueueName, taskId, 'testmsg', 1000);
  });

  app.listen(PORT, () => {
    console.log(`Process ${process.pid} listening on port ${PORT}`);
  });

Here, for each GET / request, I create a unique taskId, attach it to the `eventEmitter.once. Then I attack it to a task queue.

Internally, I launch a worker to do the task then emit the taskId event, and on reciever, I send the response for that taskId.

Now, my first request works fine. However, my second and subsequest requests fail with the error

Error: Can't set headers after they are sent.


Solution

  • Be aware that next() function can be called two times.
    Here

    if (channel) {
      next();
    }
    

    And then here

    .then(ch => {
       channel = ch;
       next();
    })
    

    Maybe the second call of next() should not be done when the first one was called, if so, try to use return statement.

    if (channel) {
      return next();
    }