Const TLS = require (' TLS '); const FS = require (' FS '); const options = { Key:fs.readFileSync (' My_key.pem '), cert:f S.readfilesync (' My_cert.pem '), //This was necessary only if using the client certificate authentication. Requestcert:false, //This was necessary only if the client uses the self-signed certificate. CA: [Fs.readfilesync (' User_cert.pem ')]};const server = Tls.createserver (options, (socket) + = { Console.log (' Server Connected ', socket.authorized? ' Authorized ': ' Unauthorized '); Socket.write (' welcome!\n '); Socket.setencoding (' UTF8 '); Socket.pipe (socket);}); Server.listen (8000, () = { Console.log (' server Bound ');});
Const TLS = require (' TLS '); const FS = require (' FS '); const OPTIONS = { //necessary only if using the client Certificat E Authentication key:fs.readFileSync (' User_key.pem '), cert:fs.readFileSync (' User_cert.pem '), Rejectunauthorized:false, //necessary only if the server uses the self-signed certificate CA: [Fs.readfilesync ( ' My_cert.pem ')]};const socket = tls.connect (8000, options, () = { Console.log (' Client Connected ', Socket.authorized? ' Authorized ': ' Unauthorized '); Process.stdin.pipe (socket); Process.stdin.resume ();}); Socket.setencoding (' UTF8 '); Socket.on (' Data ', (data) = { Console.log (data);}); Socket.on (' End ', () = { server.close ();});
Key: Excerpted from Nodejs official documentation
-out ryans-key.pem 2048;
-new -sha256 -key ryans-key.pem -out ryans-csr.pem;
-req -in ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem
Nodejs TLS is only encrypted, not authorized, further perfected