0

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.

Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92
  • doing `next()` doesn't means that you code will exit the currect closure. it will continue to run and call `queueMgr.connect()...` everytime. You should do somethink like @khalil wrote or return after next `next(); return;` – Stamos Dec 11 '17 at 10:08
  • The error you get on the second call its because its possible you call `next()` again after `connect()` and after you have called `res.end()`. – Stamos Dec 11 '17 at 10:13

1 Answers1

1

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();
}
khalil
  • 681
  • 1
  • 6
  • 22