-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·35 lines (29 loc) · 985 Bytes
/
server.js
File metadata and controls
executable file
·35 lines (29 loc) · 985 Bytes
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
var http = require('http');
var url = require('url');
var router = require('./router');
var helpers = require('./helpers');
router.register('/device', function(req, res) {
helpers.getDevices(function(device) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(device));
});
});
router.register('/devices', function(req, res) {
helpers.getDevices(function(devices) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(devices));
});
});
router.register('/switch', function(req, res) {
var params = url.parse(req.url, true).query;
helpers.changeState(params, function() {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify('device ' + params.state));
});
});
var server = http.createServer(function (req, res) {
var handler = router.route(req);
handler.process(req, res);
});
server.listen(8001);
console.log('Server running');