7 Ways to Deploy a Node.js App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll look at seven different ways to deploy a server side application i built this api with node.js and now i'm ready to share it with the world but one does not simply deploy an app to production there's all kinds of different ways to get the job done with trade-offs between cost complexity and scalability on the surface deployment seems like it should be easy we just need a computer to host the app which we connect to the internet so it can handle requests from all of our users but resources are limited and that means we need to synchronize many computers all over the world just to keep things running fast by the end of this video you'll be able to analyze the trade-offs like a cto to choose the best deployment strategy for your server side application if you're new here like and subscribe then let's jump right into it first of all let's look at some of the features in this node.js application it requires node 16 which is the current bleeding edge version and that means wherever we deploy needs to support that exact version of node it provides a basic rest api built with the express js framework nothing too special but we want these endpoints to have extremely fast response times one of the endpoints though creates a websocket connection with the client that means that the connection between the client and server needs to stay open for a long time that'll come into play later because not every deployment strategy supports long-lived connections like that now one other important feature of this server is that it's stateless that means it doesn't store any persistent data in memory or on the disk that's needed between different http requests anything related to the application state would be stored in a dedicated database now to run this app on our local machine we use the npm start command made available in the package json file now the question becomes how do we do the same thing for a computer that's accessible to the greater internet the first option is to use your own hardware you might have an old pc laying around or maybe you want to invest in your own server rack or you can even use a 100 raspberry pi like i showed in this video this was actually a common way to deploy a web application 15 years ago because back in those days the cloud didn't exist yet almost every startup today will use the cloud because it's convenient and inexpensive but deploying to your own hardware has some unique advantages most importantly you own your infrastructure it's not owned by a tech company like amazon or google who can sync you at any moment if they don't like the cut of your jip but managing your own hardware is a major pain not only do you need to think about the server configuration itself but you also have to worry about things like electricity air conditioning natural disasters and things like that if the electricity or internet goes out then your website goes down another challenge is that if you're an individual working out of your garage you'll need to request a static ip address from your internet service provider if they don't give you one then you're pretty much screwed now as your app grows in popularity the only way to scale is to buy more computers and ideally those computers are located in different parts of the world which means you'll also want an international real estate portfolio luckily there is an easier way to deploy a node.js application and it's called the cloud i'm going to focus on google cloud platform for this video but keep in mind that there are many different cloud providers out there offering similar services one option for deploying to the cloud is to use compute engine to create a virtual machine which is basically just a computer in the cloud that you pay for by the hour and every customer on google cloud gets one free micro instance per month so the cost to get started in the cloud is way lower than if you were to buy your own physical hardware after creating a new vm on compute engine it'll take a few steps to get your app deployed first you'll need to ssh into it to interact with the operating system install your preferred node.js version then clone your source code to the file system from there you can start the application then go to the firewall settings and open up port 8080 so it can be accessed on the internet and now technically your app is deployed but there are a lot of things missing here for one you'll need to reserve a static ip address then update your dns settings to point a custom domain to it in addition you'll need to manage your own ssl certificate to serve it over https and another problem is that it's not going to scale automatically once the cpu and memory has been fully utilized the app will just stop working we could address that by bringing in nginx or setting up load balancers but it's a lot of extra configuration work that most people don't want to deal with so the next option we'll look at is the app engine standard environment for node.js which is a platform as a service it makes it incredibly easy to get our local node app to a production server and the cloud fully manages the virtual machines which means your app scales automatically as more traffic comes in all that manual configuration we had to do in the virtual machine is unnecessary in app engine because it's pre-configured to scale popular types of applications first we install the google cloud sdk on our local system second we create an app.yaml file in the root of the project inside the file we specify the runtime as node 14. we can also use this file to customize the hosting behavior and set limits on how the infrastructure will scale then the third step is to use the gcloud app deploy command which will upload the local code to the cloud then look for the npm start command in the package json to run the application then it provides a url where the actual hosted app can be accessed and notice how it's http secure because google manages the ssl certificate for you in addition app engine provides task queues cron jobs login and a bunch of other tools that you wouldn't have in a normal vm it's also important to point out that app engine is a persistent server which means it's capable of handling long live connections like websockets and it's way easier to get your app online compared to a regular vm but here's the catch the reason it's called the standard environment is because you have no control over the runtime itself that makes it easier for google to scale but if you have custom runtime dependencies or a version of node that's not supported then you won't be able to use it on the free tier now although app engine standard is free initially it becomes more and more expensive as you scale up and there may be less expensive serverless options as we'll see later the next option though is the app engine flexible environment the primary difference is that instead of a sandboxed environment you have full control over the environment with docker that means you can install any version of node.js that you'd like along with any other low-level system dependencies but at the same time the cloud will still automatically scale those vms up or down based on the incoming traffic and again this is called a platform as a service and is very similar to things like heroku or elastic bean stock on aws all you have to do is change the runtime to flex in the app.yaml file then add a docker file to the root of the project app engine will automatically manage the docker image and containers for you but in some cases developers want more control to orchestrate exactly how their infrastructure scales and that's where kubernetes comes in i once heard a wise quote about kubernetes that went something like you don't [ __ ] need kubernetes and i think that's true for the vast majority of developers out there if you remember earlier deploying to a regular vm requires a lot of configuration kubernetes also requires a lot of configuration but provides a predictable framework for scaling your infrastructure it's popular for large teams who have extreme demands on their infrastructure apps like robinhood and pokemon go use kubernetes behind the scenes but if you're just deploying a little side project then kubernetes is probably way too expensive and complicated but at the other end of the complexity and cost spectrum we have serverless functions just like the app engine standard environment cloud functions provide a set runtime and allow you to deploy your code without ever having to think about the underlying infrastructure in fact you can just copy and paste your code directly into the ui to deploy it the primary difference here compared to something like app engine is that our servers are ephemeral or short-lived and that allows google to bill us for the exact amount of resources that we use instead of paying a fixed monthly price for a server we pay a tiny amount each time a function is invoked in addition you can trigger cloud functions based on many different events that happen in the cloud like when a record is updated in a database or when a file is uploaded to a storage bucket and that makes it much easier to run background jobs compared to other approaches cloud functions are a very easy and inexpensive way to deploy a node app but there are a few caveats that you need to be aware of in our case we have an endpoint that needs to support websockets however because cloud functions are short-lived we can't really implement web sockets in them another issue you might run into is something called the cold start time when a function is first allocated in the cloud it might take a little while to get things set up and produce a very high response time on the initial request another drawback is the fact that you have no control over the runtime when using a serverless function but there's actually one more deployment option we can use to get around this limitation called cloud run it works just like a serverless function but instead of using a predefined runtime you can customize it by containerizing your application with docker allowing us to specify the bleeding edge version of node.js along with any other low-level dependencies that we might need once we're able to run this app with docker we can then upload the image to the container registry then use the image to create a new cloud run deployment and now we have all the benefits of serverless like automatic scaling and pay-as-you-go pricing while also having a lot more control over the underlying server and as an added bonus we can set a minimum number of instances to reduce the cold start time although that comes at a higher cost and with that we have seven different ways to deploy a node.js application if you want to see some of these deployment options in action consider becoming a pro member at fireship io to get access to my full courses thanks for watching and i will see you in the next one
Info
Channel: Fireship
Views: 469,183
Rating: undefined out of 5
Keywords: webdev, app development, lesson, tutorial, js, node, node.js, javascript, deploy app, 100 days of code, learn to code, top 7
Id: uEVmD6n8Il0
Channel Id: undefined
Length: 9min 14sec (554 seconds)
Published: Mon Jul 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.