Day two - envs, directory structure and db connections
Tags: node, mongodb, cpanel
Published: 2020-05-07 13:45:13

---
title: Day two - envs, directory structure and db connections
published: true
description: Third in a new series on building a full stack app using cpanel, mongodb, node and react
tags: nodejs, mongodb, cpanel
cover_image: http://toasterfaves.triplejdeveloping.com/images/logo.png
canonical_url: https://www.triplejdeveloping.com/blogarticles?blog=step-two-day-one
series: Full stack recipe app - cpanel to react
---
## 4/5/20 Step one. Still

This evening, I did a bit more work - I started on the database connection and functions. Brave, huh?

First things first - I found the connection details from Mongo Atlas for the new cluster I had created previously. Then, I set up some environment variables in my server terminal:

I edited the <span class="terminal-font">.bash_profile</span> using the text editor of my choice (nano, what of it?). I added a few exported variables in there:

*   `export MONGO_USER=<user name from mongo>`
*   `export MONGO_PASSWORD=<password from mongo>`
*   `export MONGO_PREFIX=<from the connection string>`

If you choose this route (I chose it because it is slightly more secure than plain text strings in my code base - I imagine it will go up to github at some point), don't forget to <span class="terminal-font">source ~/.bash_profile</span> after you have updated and saved it - this will make these new environment variables available to you on the server.

Next, I created a bit of a file structure. By the way, it is probably time to mention I decided to use [TypeScript](https://www.typescriptlang.org/) for this project. We use it at work, I like it, maybe you will too! Anyway, I have a main `index.ts` in the root directory. I then have a `src` directory as well. Within that, I have a `db` directory, `recipes` directory and an `interfaces.ts` file.

Inside the `db` directory, I have an `index.ts` and a `client.ts`. In a brave move, I am going to introduce some images. This will give you a better idea of the starting structure (this may be refactored later, but I am making hay while the mental sun is shining):

![structure](https://dev-to-uploads.s3.amazonaws.com/i/k4lcryta89g4vmiinhy1.png)

You can see I also added a <span class="terminal-font">recipes</span> directory with an index of its own as well.

Inside the client file, I set up a Mongo connection using those environment variables I set in the server earlier:

![connection](https://dev-to-uploads.s3.amazonaws.com/i/zc8xzbzcerwqzsnjw0fk.png)

There is nothing out of the ordinary here - I followed the [Mongo docs](https://mongodb.github.io/node-mongodb-native/3.5/tutorials/connect/) for connecting with the nodejs driver.

I guess the only sadness is not using async/await, but I am too tired to think about that. This works, and a refactor later won't hurt as both will still return a promise-y value. Let's just be happy there are no callbacks...

I did it this way because I would have a reusable connection that I could use without having to reference the variables etc. every time. You might want to do it a different way, but I have written and reusable connection this way before and like it. *shrugs*

You can see the db functions I added to use this connection in the next instalment. Same Bat-time, same Bat-channel.