node.js의 웹프레임워크인 express 모듈을 사용했고, PiFaceCAD를 다루는 Python 코드를 호출하기 위해 child_process 모듈을 사용했다.
server.js
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/', function (req, res) {
res.sendFile( __dirname + "/" + "form.html" );
})
app.get('/lcd_write', function (req, res) {
text = "\"" + req.query.text + "\""
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { console.log(stdout) }
cmd = "python3 -c \"import sys, pifacecad; pifacecad.PiFaceCAD().lcd.write(sys.argv[1])\""
exec(cmd + " " + text, puts);
console.log(text);
res.end(text);
})
var server = app.listen(8000, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
form.html
<html>
<body>
<form action="/lcd_write" method="GET">
Input text here: <input type="text" name="text"> <br>
<input type="submit" value="Submit">
</form>
</body>
</html>
댓글 없음:
댓글 쓰기