Implementing HTTP/2 with Express
So, I hope I convinced you that HTTP it's a pretty, pretty, pretty cool thing, really cool technology. It had some of the controversy related to it but I think there are more benefits to using it and definitely it's part of the most browsers so we should tap into that power. So, now let's actually take a look how we are implementing our HTTP/2 with Node.js and Express in particular. So, let's get our hands dirty. Let's open our editors. Let's type some code and get it working from scratch. So first of all, we need to create an SSL key and certificate. The easiest way to do this, especially on Macs, we can use open SSL. So, I'm creating a folder, then I'm using the command open SSL. That will create some of the files so you need to execute a few more open SSL commands and it will ask me a few questions, country, state, etc. You can leave them blank or enter some of the information because key and certificate they're not intended for production use. When you go to production, when you building a website which is facing public, you would buy this certificate and the key from a provider. So, for now we just self-signing, the last command we'll self-sign and in the end we would need two files, certificate and key. In step number two you need to pick a library to use. By default as of now the current version of Node.js it's seven. Seven doesn't have HTTP/2, it has only HTTP/1 and HTTPS, so you would need to bring one of these modules. So, luckily Core HTTP/2 is coming really, I don't know if it's coming really soon but it's coming and you could track, you could subscribe to that GitHub issue if you click on the link but right now the choices they come down to two libraries, SPDY, I like this library a lot, it works nicely with Express, it's not just for SPDY, it's actually supports HTTP/2 so the name is kind of a misnomer. We will go with that library. HTTP/2 I do not recommend. It's not working nicely with Express, so we not gonna cover it. Our second library we'll be using SPDY. So, let's go ahead and create our project, so we already have the folder, we have key and certificate so now npm init will create package.json. Inside of that folder we would install Express and SPDY and we would use dash save. Dash save, what it's doing it will also save the package version in package.json. This is a short version of dash dash save. If you want to know more tricks like that related to npm take a course on Node University on npm and I stands for install, so as just a short version of typing npm install dash dash save, Express SPDY and we will get the current versions. Now, if you're watching this course years and years from when I recorded it in late 2006, maybe the new versions of Express would not work with my code, so make sure you get the code from the Github. I will provide the link to the code on the GitHub real world 100% because I have the exact version there. So, now let's open and create index.js or you can name it server.js or app.js. In that file I'm using EX6 so I'm using const but it's okay to use var as well or let and we declaring a port number 3000 and then I'm importing SPDY and Express. Path and effect there are core libraries so we don't need to install them with npm, I'm only installing SPDY and Express. And then the last line we instantiate the Express server. Once we do it, we create our route, so the route will be get, so it will be responding to the get request and it will work for any URL. And then I'm using a fat error which is a EX6 syntax but basically it's synonymous function definition and then I'm outputting JSON message OK. Now what you want to do you want to grab those two files, key and certificate, which we created using the command line before and you want to use the fast module, a fast read file sync and I'm using dirname to make sure my code will work irrespective of the path I made and that would be an options object. We will need to pass this options object into the createServer along with our Express app and then we would execute listen. So, the key difference here between our standard HTTP or Express app is that instead of HTTP or HTTPS we're using SPDY and then we also must pass the key and certificate as we would do with HTTPS protocol and then I'm console logging the port number on which I'm listening on which my server is listening. So, now let's go ahead and start the server, Node server.