-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (47 loc) · 1.3 KB
/
index.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var strftime = require('strftime');
var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors');
var axios = require('axios');
var app = express();
var config = require('./config.json');
if (!config) {
console.error('Config not found!');
return;
}
app.use(cors());
app.use(bodyParser.json());
function handleRequest(req, res) {
if (!req.body) return;
let body = req.body;
// If an individual JSON object is passed in, put into an
// array ("batch of one")
if (!Array.isArray(req.body)) {
body = [req.body];
}
let requests = [];
body.forEach(logEntry => {
let request = axios.post(config.logstashUrl, {
...logEntry,
type: config.type,
index: strftime(config.index)
});
requests.push(request);
});
axios
.all(requests)
.then(() => {
res.send('');
})
.catch(error => {
console.error('Error connecting to logstash.', error.toString());
res.status(500).send('');
});
}
// Some logging tools like StackTrace.js POST entries to an endpoint
app.post(config.routeName, handleRequest);
// Others, like bunyan will PUT log entries
app.put(config.routeName, handleRequest);
app.listen(config.listenPort, function() {
console.log('recordatus listening on port ' + config.listenPort);
});