Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. var express = require('express'),
  2. app = express(),
  3. kafka = require('kafka-node'),
  4. Producer = kafka.Producer,
  5. client = new kafka.Client('localhost:2181/'),
  6. producer = new Producer(client),
  7. bodyParser = require('body-parser');
  8.  
  9. // create application/json parser
  10. var jsonParser = bodyParser.json()
  11.  
  12. app.post('/kafka', jsonParser, function (req, res) {
  13. console.log('From outside:');
  14. console.log(req.body);
  15. payloads = [
  16. { topic: 'node_kafka_test', messages: req.body}
  17. ];
  18. producer.on('ready', function () {
  19. producer.send(payloads, function (err, data) {
  20. console.log('From inside:');
  21. console.log(data);
  22. });
  23. });
  24. });
  25.  
  26. app.listen(3000, function () {
  27. console.log('Example app listening on port 3000!');
  28. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement