var tls = require('tls'); var fs = require('fs'); var options = { // This is necessary only if using the client certificate authentication. key: fs.readFileSync('server-key.pem'), cert: fs.readFileSync('server-cert.pem'), // This is necessary only if the client uses the self-signed certificate. //ca: [ fs.readFileSync('ca-cert.pem') ], requestCert: false, rejectUnauthorized: true }; var server = tls.createServer(options, function(test) { console.log('server connected', test.authorized ? 'authorized' : 'unauthorized'); test.write("welcome!\n"); test.setEncoding('utf8'); test.on('data', function(data) { console.log(data); }); test.on('close', function() { console.log('client has closed'); server.close(); }); }); server.listen(2345, function() { console.log('server bound'); });
var tls = require('tls'); var fs = require('fs'); var options = { host: 'localhost', port: 2345, // These are necessary only if using the server certificate authentication //key: fs.readFileSync('client-key.pem'), //cert: fs.readFileSync('client-cert.pem'), // This is necessary only if the server uses the self-signed certificate ca: [ fs.readFileSync('ca-cert.pem') ], rejectUnauthorized: true }; var client = tls.connect(options, function() { console.log('client connected', client.authorized ? 'authorized' : 'unauthorized'); process.stdin.setEncoding('utf8'); process.stdin.on('readable', function() { var chunk = process.stdin.read(); if (chunk !== null) { client.write(chunk); } });
}); client.setEncoding('utf8'); client.on('data', function(data) { console.log(data); }); client.write("happy new year!");