-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (53 loc) · 1.79 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var http = require('http');
var url = require('url');
var fs = require('fs');
var qs = require('querystring');
var SerialPort = require("serialport").SerialPort;
// Change the serial file descriptor to match yours
var serialPort = new SerialPort("/dev/tty.usbserial-A700ew3I", {
baudrate: 9600
});
serialPort.on("open", function () {
console.log('open');
serialPort.on('data', function(data) {
console.log('data received: ' + data);
});
console.log('Starting web server...');
http.createServer(function (request, response) {
var render = function(filename, contentType) {
var content = fs.readFileSync(filename);
response.writeHead(200, {'Content-Type': contentType});
response.end(content);
};
var method = request.method.toUpperCase();
var uri = url.parse(request.url, true);
var match = null;
if (method === 'GET' && uri.pathname === '/') {
render('index.html', 'text/html');
}
match = uri.pathname.match(/^\/css\/([^\/]+)$/);
if (method === 'GET' && match) {
render('css/'+match[1], 'text/css');
}
match = uri.pathname.match(/^\/js\/([^\/]+)$/);
if (method === 'GET' && match) {
render('js/'+match[1], 'application/javascript');
}
if (method === 'POST') {
var body = '';
request.on('data', function (data) {
body +=data;
});
request.on('end',function(){
params = qs.parse(body);
console.log("Passkey sent: " + params.passkey);
serialPort.write(params.passkey, function(err, results) {
console.log('err ' + err);
console.log('results ' + results);
});
render('index.html', 'text/html');
});
}
}).listen(1337);
console.log('Server running at http://127.0.0.1:1337/');
});