The source code for a public API that I run.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

50 lines
1.3 KiB

var units = require('node-units');
// might as well cache this so we dont have to compute every time
// the list of available units will only change once we restart
var available = {};
var db = units.getDB();
for(var group in db){
available[group] = Object.keys(db[group]);
}
return module.exports = function(server){
server.get('/units', function(req, res){
res.writeHead(200, {
'Content-Type': 'application/javascript'
});
return res.end(JSON.stringify(available));
});
server.get(/units\/type\/([a-zA-Z]+)/i, function(req, res){
var result = units.getUnitType(req.params[0]);
res.writeHead(200, {
'Content-Type': 'application/javascript',
});
return res.end(JSON.stringify(result));
});
server.get(/\/units\/(.*?)\/([a-zA-Z]+)\/to\/([a-zA-Z]+)/i, function(req, res){
var value = decodeURIComponent(req.params[0]);
var from = req.params[1];
var to = req.params[2];
var answer = {
'convert': value,
'from': from,
'to': to,
'result': undefined,
'error': undefined
};
try{
answer['result'] = units.convert(value + ' ' + from + ' to ' + to);
} catch(e){
answer['error'] = e;
}
res.writeHead(200, {
'Content-Type': 'application/javascript'
});
return res.end(JSON.stringify(answer));
});
};