pi-rfid-jukebox/bin/tag-reader-dummy

98 lines
2.9 KiB
JavaScript
Executable File

const express = require('express');
const bodyParser = require('body-parser');
const mustache = require('express');
var html = `
<!doctype html>
<html lang=en>
<head>
<link rel="stylesheet" href="https://unpkg.com/purecss@0.6.2/build/pure-min.css" integrity="sha384-UQiGfs9ICog+LwheBSRCt1o5cbyKIHbwjWscjemyBMT9YCUMZffs6UqUTd0hObXD" crossorigin="anonymous">
<style>
form { margin: 1em 0; }
.button-error {
color: white;
border-radius: 4px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
background: rgb(202, 60, 60);
}
</style>
</head>
<body>
<div class="pure-g" style="max-width: 768px; margin: 1em auto">
<div class="pure-u-1-1">
<h1>tag-reader-dummy</h1>
<form method="post" class="pure-form">
<input class="pure-button pure-button-primary" type="submit" name="uid" value="12121212">
OK Then.mp3 / Other.mp3
</form>
<form method="post" class="pure-form">
<input class="pure-button pure-button-primary" type="submit" name="uid" value="BADC0FFEE">
Brazil Theme.mp3
</form>
<div>
<form method="post" class="pure-form" style="display: inline-block;">
<input class="pure-button pure-button-primary" type="submit" name="uid" value="STOPSTOP">
</form>
<form method="post" class="pure-form" style="display: inline-block;">
<input class="pure-button pure-button-primary" type="submit" name="uid" value="PAUSEPAUSE">
</form>
</div>
<div>
<form method="post" class="pure-form" style="display: inline-block;">
<input class="pure-button button-error" type="submit" name="uid" value="E1">
</form>
<form method="post" class="pure-form" style="display: inline-block;">
<input class="pure-button button-error" type="submit" name="uid" value="E2">
</form>
</div>
<form method="post" class="pure-form">
<input type="text" name="uid" value="0123456789"> <input class="pure-button pure-button-primary" type="submit">
</form>
</div>
</div>
<script>
HTMLCollection.prototype.forEach = Array.prototype.forEach;
document.getElementsByTagName('form').forEach(function(form) {
form.onsubmit = function(e) {
e.preventDefault();
var uid = e.target.querySelector('[name=uid]').value;
console.log(uid);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://' + document.location.host + '/', true);
xhr.setRequestHeader('Content-Type', 'application\/x-www-form-urlencoded');
xhr.send('uid=' + uid);
};
});
</script>
</body>
</html>
`;
var app = express();
var server = require('http').createServer();
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', function(req, res, next) {
res.send(html);
});
app.post('/', function(req, res, next) {
var uid = req.body.uid;
if (uid) {
process.stdout.write(uid);
}
res.send('ok');
});
server.on('request', app);
server.listen(8191, function () {
process.stderr.write('tag-reader-dummy listening at http://*:8191');
})
// vim:ft=javascript: