-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (59 loc) · 1.87 KB
/
Copy pathindex.js
File metadata and controls
68 lines (59 loc) · 1.87 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
64
65
66
67
68
/**
* Module dependencies.
*/
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const serialport = require('serialport');
/**
* Port for sending serial data on a raspberry pi
* we will get more into this later
*/
const port = new serialport('/dev/ttyACM0', 9600, function (event) {
});
/**
* Create Express server.
*/
const app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
// ejs is something we may use late to make the page look a little prettier
app.engine('html', require('ejs').renderFile);
app.use(express.static(path.join(__dirname, 'public'))); // space for images and static stuff
/** bodyParser.urlencoded(options)
* Parses the text as URL encoded data (which is how browsers tend to send form data from regular forms set to POST)
* and exposes the resulting object (containing the keys and values) on req.body
*/
app.use(bodyParser.urlencoded({
extended: true
}));
/**bodyParser.json(options)
* Parses the text as JSON and exposes the resulting object on req.body.
*/
app.use(bodyParser.json());
/**
* Primary app routes.
*/
app.get('/', function getIndex(req, res) {
console.log('GET: /index');
res.render('index.html');
});
/*
This is where we will handle the series of commands to the bot.
On a post, we will read an array of commands and send them to
the arduino over serialport
*/
app.post('/', function handlePost(req, res) {
console.log('POST /index');
console.log("Commands: " + req.body.cmd.cmd);
port.write(req.body.cmd.cmd);
// right back to the page
res.render('index.html');
});
/**
* Start Express server.
*/
app.listen(app.get('port'), () => {
console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env'));
});
module.exports = app;