-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (65 loc) · 1.93 KB
/
server.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const flash = require('connect-flash');
let coap = require('coap')
let server = coap.createServer()
// Init App
const app = express();
// Load View Engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Body Parser Middleware
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
// Set Public Folder
app.use(express.static(path.join(__dirname, 'public')));
// Express Messages Middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
let pir1 = false, pir2 = false;
app.get('/', function(req, res){
res.render('index', {pir1, pir2});
})
app.get('/update', function(req, res){
res.json({pir1, pir2});
})
// Start Server
const port = process.env.PORT || 80;
app.listen(port, function(){
console.log(`Server started on port ${port}...`);
});
server.on('request', function(req, res) {
let resource = req.url.split('/')[1]
if (resource == 'pir1') {
if (req.payload.toString('utf8') == "0")
pir1 = false
if (req.payload.toString('utf8') == "1")
pir1 = true
}
if (resource == 'pir2') {
if (req.payload.toString('utf8') == "0")
pir2 = false
if (req.payload.toString('utf8') == "1")
pir2 = true
}
// console.log(req.payload.toString('utf8'))
res.end(`${req.url.split('/')[1]} state set to ${req.payload.toString('utf8')}`)
})
// the default CoAP port is 5683
server.listen(5683, null,function() {
// var req = coap.request('coap://localhost/Matteo')
// req.on('response', function(res) {
// res.pipe(process.stdout)
// res.on('end', function() {
// process.exit(0)
// })
// })
// req.end()
console.log('listening...')
})