Skip to content

Instantly share code, notes, and snippets.

@johndstein
Last active August 29, 2015 14:06
Show Gist options
  • Save johndstein/5482408a7ab6f25cb58b to your computer and use it in GitHub Desktop.
Save johndstein/5482408a7ab6f25cb58b to your computer and use it in GitHub Desktop.
Node SSL Example
var Promise = require('bluebird');
var https = require('https');
var config = require('config');
var fs = require('fs');
var _ = require('lodash');
function _readFileSafe(path) {
try {
return fs.readFileSync(path);
} catch (err) {
return null;
}
}
var key = _readFileSafe(config.baleen.key);
var cert = _readFileSafe(config.baleen.cert);
var options = {
host: config.baleen.shadow.host,
port: config.baleen.shadow.port,
headers: {
'api-key': config.baleen.apikey,
'Content-Type': 'application/json'
}
};
var agent = new https.Agent({
host: config.baleen.shadow.host,
port: config.baleen.shadow.port,
key: key,
cert: cert
});
function fileMd5ShadowCheck(md5, cb) {
//console.log('options', options);
var body = JSON.stringify({
files: [md5]
});
// console.log('BODY!!!', body);
var opts = _.cloneDeep(options);
opts.path = '/boo/bar';
opts.method = 'POST';
opts.headers['Content-Length'] = body.length;
console.log('OPTS', JSON.stringify(opts,null,2));
opts.agent = agent;
// opts.key = key;
// opts.cert = cert;
var buff = [];
var req = https.request(opts, function(resp) {
resp.on('data', function(data) {
buff.push(data);
});
resp.on('end', function() {
cb(null, buff.join(''));
});
resp.on('error', function(err) {
cb(err);
});
});
req.write(body);
req.end();
}
module.exports = {
fileMd5ShadowCheck: fileMd5ShadowCheck,
checkPromise: Promise.promisify(fileMd5ShadowCheck)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment