Skip to content

Instantly share code, notes, and snippets.

@tgerring
Created May 27, 2014 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgerring/d3b76644ecfa155bdf7f to your computer and use it in GitHub Desktop.
Save tgerring/d3b76644ecfa155bdf7f to your computer and use it in GitHub Desktop.
sails/express/node bridge to cpp-ethereum json rpc
/**
* MessageController
*
* @module :: Controller
* @description :: A set of functions called `actions`.
*
* Actions contain code telling Sails how to respond to a certain type of request.
* (i.e. do stuff, then send some JSON, show an HTML page, or redirect to another URL)
*
* You can configure the blueprint URLs which trigger these actions (`config/controllers.js`)
* and/or override them with custom routes (`config/routes.js`)
*
* NOTE: The code you write here supports both HTTP and Socket.io automatically.
*
* @docs :: http://sailsjs.org/#!documentation/controllers
*/
var http = require("http");
var https = require("https");
var getJSON = function(options, data, onResult)
{
console.log("rest::getJSON");
var prot = options.port == 443 ? https : http;
var req = prot.request(options, function(res)
{
var output = '';
console.log('RESPONSE STATUS: ' + options.host + ':' + res.statusCode);
console.log('RESPONSE HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('RESPONSE CHUNK:' + chunk);
output += chunk;
});
res.on('end', function() {
var obj = JSON.parse(output);
onResult(res.statusCode, obj);
});
});
req.on('error', function(err) {
res.send('RESPONSE ERROR: ' + err.message);
});
console.log('Checking for data to send');
console.log('REQUEST DATA: ' + data);
if (typeof data === 'string' ) {
console.log(data);
req.write(data, encoding='utf8');
} else {
console.log('no data or in non-string format');
}
req.end();
};
module.exports = {
/**
* Action blueprints:
* `/message/inbound`
*/
inbound: function (req, res) {
var jsonRPCObj = req.body;
var jsonstr = JSON.stringify(jsonRPCObj);
var dataLength = jsonstr ? jsonstr.length : 0;
// console.log(jsonRPCObj);
// console.log(jsonstr);
// console.log(dataLength);
var options = {
host: 'localhost',
port: 8080,
path: '/',
//agent: false,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '*/*',
'Content-Length': dataLength
}
};
getJSON(options, jsonstr, function(statusCode, result) {
console.log("onResult: (" + statusCode + ")" + JSON.stringify(result));
res.statusCode = statusCode;
res.send(result);
});
},
/**
* Overrides for the settings in `config/controllers.js`
* (specific to MessageController)
*/
_config: {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment