-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.js
46 lines (39 loc) · 1.14 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
39
40
41
42
43
44
45
46
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const dweetClient = require('node-dweetio');
const http = require('http').Server(app);
const io = require('socket.io')(http);
const moment = require('moment');
const dweetio = new dweetClient();
const dweetThing = 'node-temperature-monitor';
const SERVER_PORT = 3000;
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + 'index.html');
});
app.use((req, res) => {
res.status(404).json({
message: 'Resource not found'
});
});
io.on('connection', (socket) => {
console.log('Connection has been established with browser.');
socket.on('disconnect', () => {
console.log('Browser client disconnected from the connection.');
});
});
dweetio.listen_for(dweetThing, (dweet) => {
const data = {
sensorData: dweet.content,
time: moment().format('HH:mm:ss')
};
io.emit('sensor-data', data);
});
http.listen(process.env.PORT || SERVER_PORT, () => {
console.log(`Server started on the http://localhost:${SERVER_PORT}`);
});