To check how to deploy in another way with Github read here
- create heroku account
- it's a cloud platform for hosting apps
- https://signup.heroku.com/
- create application
- provide a name (it will be the url https://app-name.herokuapp.com)
-
install heroku CLI
- download here: https://devcenter.heroku.com/articles/heroku-cli
-
prepare app inside the project in your command line terminal (iterm/ powershell/vsCode terminal)
$ heroku login
$ heroku git:remote -a app-name
- make sure package.json is in the root of the project
- make sure package.json contains a start script
"scripts": {
"start": "node server.js"
},
- Create a new cluster on mongoDB atlas (Please remember your username, password when you create them the first time for your account)
-
That url will look something like this
mongodb+srv://manish:<password>@cluster0-st12d.mongodb.net/<dbname>?retryWrites=true&w=majority
-
Change
<password>
with your real password and<dbname>
with the database name you need for your project -
It will then look like this
mongodb+srv://manish:Test@123@cluster0-st12d.mongodb.net/secondProject?retryWrites=true&w=majority
-
copy this mongodb url and save it somewhere
-
configure app
- REMOVE ALL API KEYS (e.g. secret, clientIDs) FROM YOUR CODE
- Use the
dotenv
package if you've not used to be able to access environment variables viaprocess.env
$ npm install --save dotenv
- add
require('dotenv').config();
to app.js - create
.env
file (adds environment variables toprocess.env
) - add
.env
to.gitignore
(To ensure we never commit out .env file to github) - replace hardcoded mongodb URI with process env variable in app.js
#These .env variables are used to work locally, to make them work in your deployed app you need to set them up in heroku as well.
-
Go to your heroku dashboard app page and go to the Settings Tab
-
Add all the envs variable that connect to your Database or any external API's there.
-
To deploy
$ git add
&&$ git commit
$ git push heroku master
-
If your application isn't deployed successfully do
$ heroku logs
$ heroku logs -t
You can also share your cluster with other team members so that they can check the data in MongoDB Compass
- Go to your project directory, open the terminal and type
heroku logs