-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
37 lines (33 loc) · 963 Bytes
/
api.js
File metadata and controls
37 lines (33 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var bodyParser = require('body-parser');
var express = require('express');
var kafka = require('kafka-node');
// Use Zookeeper connect String in Client:
var client = new kafka.Client('10.60.3.132:2181','test-client');
var producer = new kafka.Producer(client);
producer.addListener('ready', function () {
console.log('Kafka producer is ready');
});
app = express();
app.use(bodyParser.json());
/*app.use(bodyParser.urlencoded({
extended: true
}));
*/
app.post('/logs/', function(req, res) {
//console.log(req.body);
if(producer) {
payload = [
{ topic: 'logs', messages: JSON.stringify(req.body)}
];
producer.send(payload, function (err, data) {
if (err) {
res.send(500, err);
} else {
res.send(200, 'Message is queued...');
}
});
} else {
res.send(500, 'Producer is not initialized');
}
});
app.listen(8083);