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?