Recently I have had to work with SSL certificates a lot and have just finished adding a SSL certificate into Node.js. One of the services I am using requires two-way SSL I forgot how much I liked coding in Node.js, I haven’t got the opportunity to do it recently. I decided the learning would make a nice blog post…
Create a new directory to hold your Node.js application.
You need some modules to implement two-way SSL in Node.js. You of course want a package.json file to handle dependencies and make it easy for future contributors of your application to get the necessary dependencies. We are going to create a package.json file and download and add the necessary dependencies to the newly created package.json file.
Assuming you have PKCS 12 file (.p12 file) called certificate.p12 you need to extract a public certificate and private key. You will want to copy the generated privateKey.pemand publicCert.pem to a new directory resources/keys inside the NodeApp directory.
With this done we are ready to finish up the Node.js app. Create a file server.js in the NodeApp folder and add the following code.
Lines 15, 16: load the private key and public cert. Line 17: you are required to add a passphrase when you generate the private key so whatever password you used enter the appropriate password here.
A pretty trivial example but you should now have a working implementation of two-way SSL in Node.js.