Last active
August 29, 2015 13:58
-
-
Save bodokaiser/9961189 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var http = require('http'); | |
var express = require('express'); | |
var app = express(); | |
app.use(express.static(__dirname + '/../srv')); | |
fs.writeFile('/sys/class/gpio/export', '34', function(err) { | |
if (err) throw err; | |
app.get('/light', function(req, res, next) { | |
fs.readFile('/sys/class/gpio/gpio34/value', function(err, result) { | |
if (err) return next(err); | |
res.status(200); | |
res.format({ | |
json: function() { | |
res.json({ on: Boolean(result.toString()) }); | |
} | |
}); | |
}); | |
}); | |
app.put('/light', function(req, res) { | |
var state = (res.body.on) ? '1' : '0'; | |
fs.writeFile('/sys/class/gpio/gpio34/value', state, function(err) { | |
if (err) return next(err); | |
res.status(200); | |
res.format({ | |
json: function() { | |
res.json(); | |
} | |
}); | |
}); | |
}); | |
}); | |
app.use(express.errorHandler()); | |
http.createServer(app).listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment