Below i highlight every single step you need in order to deploy your Node, Mongodb app to heroku:
- Assumming your code works on localhost
- Sign Up to www.heroku.com and setup your billing information (it won't charge anything but we need it to create a MongoDB add-on to our app on heroku. It won't accept without billing info)
- On root create a
.env
file. Inside writeMONGO_DB_URI=mongodb+srv://<YourUsername>:<yourPassword>@cluster1.qcpvo.mongodb.net/<dbname>?retryWrites=true&w=majority
(Your own uri without any string " ") - Then change in index.js (or server.js based on your setup):
const uri = 'mongodb+srv://YourUsername:<yourPassword>@cluster1.qcpvo.mongodb.net/Students?retryWrites=true&w=majority';
TOconst uri = process.env.MONGO_DB_URI || 'mongodb+srv://Ulan:123@cluster1.qcpvo.mongodb.net/Students?retryWrites=true&w=majority';
- On root directory create Procfile file.
- On terminal run:
touch Procfile
- Inside Profile write
web: npm start
- On terminal run these commmands by order:
git init
git add --all
git commit -m" deployment"
-
heroku login
(it will open a browser to confirm login. Hit login and get back to terminal) -
heroku create your-app-name
(this will output something like this: Creating ⬢ your-app-name... done) -
heroku addons:create mongolab:sandbox
(It installs mLab MongoDB add-on with the "Sandbox" plan which is free) heroku config:set MONGO_DB_URI='mongodb+srv://Ulan:<yourPassword>@cluster1.qcpvo.mongodb.net/Students?retryWrites=true&w=majority'
-
git push heroku master
(push it to heroku master) -
heroku open
(or open from heroku app dashboard)
- If you created routes try CRUD operations with your new deployed link.
P.S: mLab is shutting down its Heroku add-on on November 10, 2020. You will lose access to your data unless you detach from Heroku or migrate to MongoDB Atlas before then.
Top comments (0)