add route function

This commit is contained in:
AJ ONeal 2018-10-15 21:06:59 -06:00
parent be7a895dc7
commit 26939a62cf
1 changed files with 80 additions and 53 deletions

View File

@ -599,6 +599,7 @@ function handleApi(req, res) {
)); ));
} }
function route() {
if (/\b(config)\b/.test(opts.pathname) && /get/i.test(req.method)) { if (/\b(config)\b/.test(opts.pathname) && /get/i.test(req.method)) {
getConfigOnly(); getConfigOnly();
return; return;
@ -657,6 +658,32 @@ function handleApi(req, res) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}})); res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}}));
} }
if (!req.headers['content-length'] && !req.headers['content-type']) {
route();
return;
}
var body = '';
req.on('readable', function () {
var data;
while (true) {
data = req.read();
if (!data) { break; }
body += data.toString();
}
});
req.on('end', function () {
try {
opts.body = JSON.parse(body);
} catch(e) {
res.statusCode = 400;
res.end('{"error":{"message":"POST body is not valid json"}}');
return;
}
route();
});
}
function serveControlsHelper() { function serveControlsHelper() {
controlServer = http.createServer(handleRemoteClient); controlServer = http.createServer(handleRemoteClient);