Set-up the Backend

Please follow these instructions to configure and run the Backend on your localhost

Within terminal (Mac) or cmd (windows) navigate into the 'Backend' project directory and install the dependencies

cd <backend-dir> yarn install OR npm install

Configuration

Before we can run our backend web app we need to configure some important settings.

To make our project easier to maintain, we are using a package called 'config' which allows you to define a set of default parameters, and extend them for different deployment environments (development, staging, production, etc.).

For more information, please see: https://www.npmjs.com/package/config

Please open the following files within your chosen text editor.

  1. config/default.json

  2. config/custom-environment-variables.json

We define static parameters within 'default.json' and then use 'custom-environment-variables.json' to map node env variables to parameters that contain more sensitive data such as passwords or anything we don't want to hardcode into our app.

Configure our mail server

We are using a package called 'nodemailer' to handle sending password recovery emails. Follow these instructions to configure your mail server:

Within 'config/default.json' change the following static parameters:

{
    "jwtPrivateKey": "", <<-- Leave blank
    "mailServer": {
        "host": "-HOST-", <<-- Enter your mail server host
        "auth": {
            "user": "", <<-- Leave blank
            "pass": "" <<-- Leave blank
        },
        "from": "-FROM-EMAIL-", <<-- Enter a from email address
        "subject": "Password Reset" <<-- Change if you wish
    }
}

Please note: if you are unsure about any of this information, please contact your mail server provider.

Node Environment Variables

As we previously explained, we are using Node Environment Variables to supply our app with some more sensitive information. This includes our mail server user & password. In addion to our JWT Secret.

To define, please go back to your terminal or cmd and write the following commands

export mailServer_user=<Your-mail-server-user>
export mailServer_pass=<Your-mail-server-password>
export jwt_secret_key=<Your-json-web-token-secret-key>

Please note: Windows users should use 'set' instead of 'export'

Set-up MongoDB (Setting up your database)

This web app uses MongoDB to save, query and process data. Whilst you may have a preference of your service provider (AWS, Microsoft Azure etc…) we will be using ‘mlab’.

Please visit ‘mlab’ register/login to your account.

Link- https://mlab.com/

  1. From your control panel select ‘Create new’

  2. Choose any of the Cloud Provider’s

  3. Select the appropriate ‘Plan Type’ for you. For now, let’s select the ‘Free’ plan

  4. Click Continue

  5. Select the ‘Region’ that’s closest to your location and click continue

  6. Enter a new database name and click continue

  7. Confirm your selections and click ‘Submit order’ (Free)

The database will now be created, and you will have been redirected back to your dashboard.

Adding a User to your database

  1. Under ‘MongoDB Deployments’ click on the database you just created

  2. Select ‘Users’

  3. Select ‘Add database users’

  4. Enter a Username and Password and click ‘Create’

Please remember this information as we will need it later.

Connecting our Registration System to our Database

We need a ‘connection string’ provided by ‘mlab’ to link our Registration System with our Database.

Please ensure that you are still navigated inside the database you just created.

It looks something like this:

To connect using a driver via the standard MongoDB URI:
mongodb://<dbuser>:<dbpassword>@... <<-- Copy this

Copy this URL as we will need it later.

Configuring our Registration System to Connect to our Database

  1. Open the project folder within your chosen text editor

  2. Open ‘startup/db.js'

  3. Paste the connection string obtained from ‘mlab’ into the connect method:

mongoose.connect('mongodb://<dbuser>:<dbpassword>@...')

Update the URL to contain the details of the User we added to the database in the previous steps.

mongoose.connect('mongodb://someUser:somePassword@...')

Configure cors

As you know, the Frontend and Backend are two separate web apps. As a result we need to grant access (whitelist) our frontend to communicate with our backend.

Open: 'webConfig.json'

{
    "frontend_url": "http://localhost:8080"
}

Please ensure that before running on localhost or deploying you update 'frontend_url' to match the URL hosting your Frontend web app.

By default your Frontend/Backend will be running on:

Frontend: http://localhost:8080
Backend: http://localhost:3000

Please ensure you have configured this correctly each time you run or deploy on a different environment.

Lunch our Backend Web App

yarn run dev OR npm run dev 

Your backend app should be running on

http://localhost:3000

Last updated