Instructor: We want to create a local SSL certificate for local host. We want it to be trusting Chrome and we want it to work with our Node Express applications. First thing we're going to do is fire up PowerShell in Visual Studio Code in this case. We've got this for Windows only. I enter the PowerShell command.
The certificate has now been created and stored in my local certificate store. Let's have a look at it. The next thing we want to do is fire up the Microsoft Management Console, MMC, via the command prompt. Once you have it open we need to add a new snap in, so we'll go to add new snap in.
We will select certificates. Make sure you select computer account. Local computer is the default. We'll leave it at that. Do finish, OK. Then we'll expand the certificates. Look into personal. Here we can see we've got local hosts. Then we'll click to open the actual certificate up. It gives you information about the certificate itself.
What we want to do is for Node we need to actually separate out or change the format from what is a PBX file into a separate certificate and key. First thing we do is copy to file. This is going to bring up the certificate export wizard.
We'll say next. We do want the key. Say next. Leave this ticked as default. Because it's got a key inside, we need to give it a password. Just give it a password you can remember. Hit next and you've got to decide where you want to save it to. I'll browse to where my project is going to be in here.
This is a PFX file. We will call it in this case egghead. Next finish and the file is exported. Let's go back to our Node server. I've got my project open with the Node file and the PFX file inside. What I need to do now is use open SSL to split the file out into a private key and certificate.
First one we'll do is the key. The command is open SSL. Pass it the name of your PFX file. Give it a name with the key. Now into the password you set previously. Now we'll deal the certificate. It's a similar command. Again, enter the password.
Now we can go ahead and set up our Node Express server page to use as a cell. We have a standard Express application. What we're going to do now is enhance it to use SSL and our local SSL certificate. First thing we're going to do is include the HTTPS package. We're also going to need the FS package as we're going to be looking at files.
We're going to set up some SSL options. It's going to be an object. It's going to need the key. We'll read this in from the file system. We need the public certificate. What we're going to do, we're going to change this HTTP listen method to using the SSL package, passing in the HTTPS options.
Our server should start up fine now. Let's just run it. It started up OK. I have a public folder here and we'll just have a look at the index file and see what it looks like from a local host. We open our browser to https://localhost:8765 in Chrome and we hit this, "Your connection is not private." Chrome actually isn't trusting our certificate at the moment.
Let's fix that. What we need to do is fire up the MMC console again, add in our certificate snap in, go into personal certificates, right click on our local host, copy it, go to trusted roots certificate authorities certificates and paste it in there. That gets added.
Now if we relaunch Chrome it should be trusted. If I reopen the browser, put in my host file, the HTTPS, and this time Chrome loads it fine. You can see we've got a nice secure connection.