0

I want to build a NodeJS based API that is backed with a pub-sub paradigm with e.g. Kafka. Here is a skeleton of what I want to do.

const express = require('express')
const serverApp = express()
serverApp.get('/book/:bookId', (req, res) => {
    producer.send(JSON.stringify({
        action: 'get',
        message: req.params.bookId
    }))
    consumer.on('message', (data) => {
        res.status(200).send(JSON.parse(data))
    })
})

With the option above, the first invocation works but, the subsequent ones keep failing with ERR_HTTP_HEADERS_SENT.

Keeping the consumer.on outside of serverApp.get will need to have the req and res co-ordinated.

How do I implement such an API?

cogitoergosum
  • 2,309
  • 4
  • 38
  • 62

1 Answers1

4

For example as skeleton

const express = require('express')
const serverApp = express()

const responses = Object.create(null);

consumer.on('message', (data) => {
    const result = JSON.parse(data)
    const res = responses[result.replyId]
    res.status(200).send(result)
});

serverApp.get('/book/:bookId', (req, res) => {

    const replyId = Math.random().toString(36).substr(2);    
    responses[replyId] = res;

    producer.send(JSON.stringify({
        action: 'get',
        replyId,
        message: req.params.bookId
    }))
})
Yaroslav Gaponov
  • 1,997
  • 13
  • 12