Hello,
Today I want to share with you very quick tutorial. If you are developer like me, you know that the deployment can be a headache. We always search for the easiest way to put our code online.
Usually, I use Heroku or Vercel, but last years I moved to DigitalOcean App Platform and it is amazing. It is very fast and professional. Many people think DigitalOcean it expensive, but if you want , you can host your Node.js and MongoDB application for free (using the free credits).
Here is how to make it in just 5 minutes.
What you need before we starting
Before we jump into the console, make sure you have this things ready:
A GitHub account: Your code must be push to a repository for example we will use this open source erp crm based on nodejs react : https://github.com/idurar/idurar-erp-crm
DigitalOcean Account: If you don't have one, you can get $200 free credit for 60 days with this referral link to . This is how you can host it nodejs for free!
Prepare your Node.js code
First, you must ensure your package.json is correct. DigitalOcean App Platform search for the start script to know how to run your server.
Make sure it has engines and scripts like this:
JSON
....
"engines": {
"npm": "10.2.4",
"node": "20.9.0"
},
"scripts": {
"start": "node src/server.js",
"dev": "nodemon src/server.js --ignore public/",
...
},
...
Also, don't hardcode your port. You should use process.env.PORT in your code so DigitalOcean can assign the port automatically.
Create the App in DigitalOcean
In the left menu, click on App Platform.
Click on the big blue button that say Create App.
You need to give permission to DigitalOcean to access your repositories.
Select the repository which you want to deploy.
Configuration (Important!)
DigitalOcean is smart. It will detect that you use Node.js automatically. However, you need to check few things on the configuration screen:
Environment Variables: This is the most important part!
Because we are not committing our .env file to GitHub (never do that!), you must add your secrets here. Click on Edit next to Environment Variables.
Add your env variables here. Copy connection string from your MongoDB Atlas dashboard and paste it.
Note: Make sure to allowed access from anywhere (0.0.0.0/0) in your MongoDB Atlas network settings, otherwise DigitalOcean cannot connect to database.
Build and Deploy
After you put the variables, click Next.
DigitalOcean will ask about the plan. Since we use the free credits (or if you have static site), you can select the Basic or Starter plan. For Node.js backend, the Basic plan is around $5/month, but with the $200 credit, it is effectively free for long time for testing.
Click Create Resources.
It usually take 2 or 3 minutes to build the container and deploy it.
At the top of screen, there is "Live App" link. This is your public URL. Copy it and test it on Postman or your browser.
I hope this article help you to save time and money. If you have any problem with the connection, let me know in comment section!


Top comments (0)