Reputation: 2471
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?
Upvotes: 0
Views: 586
Reputation: 2099
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
}))
})
Upvotes: 3