-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndearp.js
79 lines (56 loc) · 1.91 KB
/
ndearp.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
const express = require('express');
const app = express();
const fs = require('fs');
var bodyParser = require('body-parser');
//install node-arp by using 'npm install node-arp' in nodeServer
const arp = require('node-arp');
app.use(bodyParser.json());
app.use(express.static('public'));
app.post('/api/write',function(req,res){
//Fetch MAC address
arp.getMAC(req.connection.remoteAddress, function(err, mac) {
if (!err) {
var MACaddress = mac;
//Check user already exist
var obj;
var content = fs.readFileSync('data.json', 'utf8');
obj = JSON.parse(content);
isUserExist = obj.users.includes(MACaddress);
if(isUserExist === true)
{
res.status(404).send('User already exist')
}
else{
//Add user MAC into data.JSON
obj.users.push(MACaddress);
//increment values
obj.scores[0].option1 = obj.scores[0].option1 + req.body.scores[0].option1;
obj.scores[1].option2 = obj.scores[1].option2 + req.body.scores[1].option2;
obj.scores[2].option3 = obj.scores[2].option3 + req.body.scores[2].option3;
obj.scores[3].option4 = obj.scores[3].option4 + req.body.scores[3].option4;
//update data.json
fs.writeFile("data.json",JSON.stringify(obj),function(err){
if(err) throw err;
console.log('Saved!')
});
res.status(200).end();
}
}
else{
console.log(err);
res.end();
}
});
});
app.get('/api/read',function(req,res){
var data;
fs.readFile('data.json', 'utf8', function (err, data) {
if (err) throw err;
data = JSON.parse(data);
res.send(JSON.stringify(data));
});
})
app.get('/result',function(req,res){
res.sendFile(__dirname +'/public/result.html');
});
app.listen(3000,function(){console.log('Server listening')})