Last active
August 29, 2015 14:06
-
-
Save johndstein/5482408a7ab6f25cb58b to your computer and use it in GitHub Desktop.
Node SSL Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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