forked from lucj/IoT-demo-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
38 lines (34 loc) · 1.13 KB
/
app.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
// Load dependencies
const express = require('express'),
Influx = require('influx'),
bodyParser = require('body-parser'),
winston = require('winston');
// Create express application
let app = module.exports = express();
// Create a client towards InfluxDB
let influx = new Influx.InfluxDB({
host: process.env.INFLUXDB_HOST,
database: 'iot'
});
// Body parser configuration
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Handle inconming data
app.post('/data',
function(req, res, next){
influx.writePoints([
{
measurement: 'data',
tags: { type: req.body.type },
fields: { sensor_id: req.body.sensor_id, value: req.body.value },
timestamp: new Date(req.body.ts).getTime() * 1000000
}
]).then(() => {
winston.info(req.body);
return res.sendStatus(201);
})
.catch( err => {
winston.error(err.message);
return res.sendStatus(500);
});
});