ZeroTierOne/tests/http/server.js

63 lines
1.6 KiB
JavaScript

// ---------------------------------------------------------------------------
// Customizable parameters:
var SERVER_PORT = 18080;
// ---------------------------------------------------------------------------
var fs = require('fs');
var express = require('express');
var app = express();
app.use(function(req,res,next) {
req.rawBody = '';
req.on('data', function(chunk) { req.rawBody += chunk.toString(); });
req.on('end', function() { return next(); });
});
var knownAgents = {};
app.get('/:agentId',function(req,res) {
var agentId = req.params.agentId;
if ((!agentId)||(agentId.length !== 32))
return res.status(404).send('');
knownAgents[agentId] = Date.now();
return res.status(200).send(JSON.stringify(Object.keys(knownAgents)));
});
app.post('/:testNumber/:agentId',function(req,res) {
var testNumber = req.params.testNumber;
var agentId = req.params.agentId;
if ((!agentId)||(agentId.length !== 32))
return res.status(404).send('');
var receiveTime = Date.now();
var resultData = null;
try {
resultData = JSON.parse(req.rawBody);
} catch (e) {
resultData = req.rawBody;
}
result = {
agentId: agentId,
testNumber: parseInt(testNumber),
receiveTime: receiveTime,
results: resultData
};
testNumber = testNumber.toString();
while (testNumber.length < 10)
testNumber = '0' + testNumber;
fs.writeFile('result_'+testNumber+'_'+agentId,JSON.stringify(result),function(err) {
console.log(result);
});
return res.status(200).send('');
});
var expressServer = app.listen(SERVER_PORT,function () {
console.log('LISTENING ON '+SERVER_PORT);
console.log('');
});