-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
73 lines (60 loc) · 1.95 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
66
67
68
69
70
71
72
73
var app = require('http').createServer(handler);
var url= require('url');
var fs = require('fs');
var os = require('os');
var io = require('socket.io').listen(app);
var serialport = require("serialport");
var SP = serialport.SerialPort;
var serialPort = new SP("COM4",
{
baudrate: 115200,
parser: serialport.parsers.readline("\n")
}, false);
app.listen(5000);
/* SERIAL WORK */
serialPort.open(function (error) {
if ( error ) {
console.log('failed to open: '+error);
} else {
console.log('open');
serialPort.on('data', function(data) {
console.log('data received: ' + data);
io.sockets.emit('serial_update', data);
});
//serialPort.write("ls\n", function(err, results) {
// console.log('err ' + err);
// console.log('results ' + results);
//});
}
});
// Http handler function
function handler (req, res) {
// Using URL to parse the requested URL
var path = url.parse(req.url).pathname;
// Managing the root route
if (path == '/') {
index = fs.readFile(__dirname+'/three.html',
function(error,data) {
if (error) {
res.writeHead(500);
return res.end("Error: unable to load three.html");
}
res.writeHead(200,{'Content-Type': 'text/html'});
res.end(data);
});
// Managing the route for the javascript files
} else if( /\.(js)$/.test(path) ) {
index = fs.readFile(__dirname+path,
function(error,data) {
if (error) {
res.writeHead(500);
return res.end("Error: unable to load " + path);
}
res.writeHead(200,{'Content-Type': 'text/plain'});
res.end(data);
});
} else {
res.writeHead(404);
res.end("Error: 404 - File not found.");
}
}