-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (52 loc) · 1.43 KB
/
server.js
File metadata and controls
63 lines (52 loc) · 1.43 KB
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
/** API
*
* Smart Tank Application Interface.
*
*/
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var app = express();
var status = {
"crawler": {
"connected": 0,
"message": "Crawler not connected.",
"servo": 0,
"brake": 0,
"battery" : 0,
"sonar": 0,
"wheels": {
"fl": 0,
"fr": 0,
"rl": 0,
"rr": 0
}
}
}
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(express.static('static'));
app.get('/', function(req, res) {
/** Homepage */
res.sendFile(path.join(__dirname + '/index.html'));
})
app.post('/api/update/', function(req, res) {
/** Update latest crawler data */
status.crawler = req.body.crawler;
res.setHeader('Access-Control-Allow-Methods', 'POST')
res.sendStatus(200);
})
app.get('/api/status/', function(req, res) {
/** Return crawler information */
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', 'http://capstone-crawler.s3-website-us-west-2.amazonaws.com/');
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
data = JSON.stringify(status)
res.send(data);
})
var port = process.env.PORT || 3000;
var server = app.listen(port, function () {
console.log('Server running at http://127.0.0.1:' + port + '/');
});