How to Deploy a NodeJS App in 2020
Nodejs is one of the most popular backend languages you can write your API in. On the browser, we are used to writing code with import/export syntax (ECMAScript modules) finally we can do the same with the newer versions of node. This article shows you how to deploy your NodeJS API online and how to update it with simple steps.
Getting Started With NodeJS
- Install git
- Install NodeJS (v14+)
- This guide requires a free GitHub account
- This guide requires a free KintoHub account
Deploy an App To The Cloud
The first thing we need to do is copy the example code onto your GitHub account. You can do this by going to the following repository and click Use this template at the top right.
Once you have created the nodejs-api example within your Github account, login to your KintoHub account and do the following:
- Click on the Create Service at the top right
- Choose Backend API from the service type list
- Connect and give access to your nodejs-api on your Github account. Then select that repository.
- Change the Language from
Dockerfile
toNodeJS
- Select
14
in the Language Version - Set the Build Command to
npm install
- Set the Start Command to
npm run prod
- Set the port to 80
- Click Deploy at the top right.
After one minute, your app should be built and deployed and accessible with a URL at the bottom of the logs.
Now click on the Public API and add /hello/stranger
and you will get the following response :
{“message”:”Hello stranger”}
Update your NodeJS App
Now let’s say you want to make some changes to your app and you want to push an update to your users. First, we need to clone the code to your local machine. Make the changes, then push the new code.
- You need to clone the repo. Go to your Github repository and click on Code and then copy the URL
- Open a terminal and run
git clone {paste-your-repo-url-here}
- Open up the index.js file and change
/hello/
to/goodbye/
Test Your NodeJS App Locally
Before pushing any code, you must test it! To test your NodeJS app, run the following command.
- Run
npm install
- Run
npm start
- Open your browser to https://localhost:8000/goodbye/stranger and you should see a successful response.
Now that we know your code works, you can push it to Github with the following commands:
git add . && git commit -m ":alien: refak(App): goodbye endpoint" && git push
Deploy Your changes
Finally, we need to deploy the changes you just tested which should be working correctly. Go back to KintoHub and do the following.
- Click on your nodejs-api service
- Click Trigger Deploy at the top right.
After a minute, you should see again a successful message at the bottom with the URL to access. When you trigger a deployment on KintoHub, it does not take down your old version until your new version is live and traffic has been redirected to it. This is known as a no-downtime deployment.
What’s Next?
KintoHub comes packed with many additional features to debug and enrich your NodeJS App.
- Optimize your costs or scale your app with Advanced Settings
- Set up a Custom Domain to have your own custom URLs.
- View your Console Logs to debug any issues.
Conclusion
That’s it! Within a few minutes, you should have deployed and updated your NodeJS App. If you have any issues with this example or want to learn more, please join us on Discord. There are also more advanced guides that can be found here.