Deploying a NodeJS App to Zeit with a remote database
Hello Again,
Most Developers have issues hosting their apps while its being built for testing purposes, so this article would guide you on how to host your NodeJS App on Zeit and use a remote database powered by mlab, so lets get started.
Setup our Remote Database
We would be using mlab as our remote database provider, I’d be guiding you on how to set up a mlab account.
To create a new account and set up a database, you will need to go here. I’ve provided the images below as a reference guide on setting up your database.
Check the top of your dashboard you should see your credentials, in my case:
replace <dbuser>
and <dbpassword>
with the credentials of the user you just created
Install Zeit
Download Zeit from here
when you have Zeit Installed on your machine, run it and follow to initial setup prompt.
Clone Our Test Repo
Clone our test repo from here using this command
git clone https://github.com/ichtrojan/essential-kit
Deploy to Zeit
Zeit would not recognize any .env
file or any environments file, we have to specify our DB_HOST
in the now
command
now -e DB_HOST=mongodb://<dbuser>:<dbpassword>@ds119018.mlab.com:19018/zeit
don’t forget to replace <dbuser>
& <dbpassword>
with the credentials of the user you created earlier.
visit the link generated, here’s my link
Now lets test the database, click on sample form
I am gonna submit the following details, you can submit whatever you like
After Submitting whatever you fill in the form, check your mlab dashboard, you’d find out that a new collection has been created called users
When we click on that collection we should see the details of what you filled in the form earlier, which means everything works.
At this point you should be proud of yourself, you now know how to:
- Setup a remote MongoDB with mlab
- Clone a Github repo (if you had no idea)
- Deploy a NodeJS App to Zeit
- Setting Environments Variables on Zeit
Don’t Forget to star the Repo we used here, you can contribute to the project also.
Bye