Snow Plane Node.js Coding
Not sure what I was thinking, but I’m sure I was thinking something along the lines of Node.js, Risk and more. Maybe the skiing will assist my thought process 😉 Maybe Cube is in there somewhere as well.
var http = require('http');
var sys = require('util');
var querystring = require('querystring');
var fs = require('fs');
var dispatcher = require('./dispatcher.js');
var portSvr=8000;
var webSvr = http.createServer(function (req, res) {
try {
console.log('Incoming Request from: ' +
req.connection.remoteAddress +
' for href: ' + req.url
);
dispatcher.dispatch(req, res);
} catch (err) {
sys.puts(err);
res.writeHead(500);
res.end('Internal Server Error');
}
});
webSvr.listen(portSvr, '127.0.0.1');
console.log('Server running at http://127.0.0.1:'+portSvr+'/');
function SendJSONRisk(codestring) {
var post_options = {
host: '127.0.0.1',
port: portSvr,
path: '/riskdata',
method: 'POST',
headers: {
'Content-Type': 'application/jsonrequest',
'Content-Length': codestring.length
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
// post the data
post_req.write(codestring);
post_req.end();
}
webSvr.on("listening", function() {
var options = {
host: '127.0.0.1',
port: portSvr,
path: '/',
method: 'GET'
};
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
fs.readFile('./couchDB/fx.json', function(error, content) {
if (error) {
console.log("Got error: " + error.message);
} else {
SendJSONRisk(content);
}
});
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
});
this.dispatch = function(req, res) {
if (req.url == "/riskdata") {
console.log("dispatcher:Got response: " + res.statusCode);
req.on('data', function (chunk) {
var risk = JSON.parse(chunk);
console.log("Received risk for TradeId :" + risk.tradeId + " " + risk.yieldCurve);
process.exit(code=0);
});
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Results\n');
}
}
