req
req
res
res
Client
Server
Database
React
HTML
CSS
JS
Node.js
Express
PostgreSQL
Axios
Massive
Heroku is a platform for hosting databases, and we will use it to host our PostgreSQL databases.
Follow the link below and create an account:
SQL Tabs is an application that allows us to interact with a hosted database.
Download it here:
When dealing with sensitive information, such as API Keys and Database URL's, you want to ensure that access to that information stays private.
Dotenv is a package that allows you to create environmental variables that are only accessible on your local machine.
To install it, run the following command:
npm install dotenv
Once installed, create a .env file at the root of your project directory:
As soon as it is created, put the .env file in your .gitignore file to avoid pushing environmental variables to GitHub.
In your .env file, you can store environmental variables like so:
SERVER_PORT=4040
CONNECTION_STRING=database_url_here
Note: Do NOT put semicolons at the end of these variables.
Once environmental variables have been created, require dotenv at the top of your main server file like so:
require('dotenv').config()
Afterward, you can destructure values off of the process.env object that dotenv will create for you.
const {SERVER_PORT, CONNECTION_STRING} = process.env;
Massive is a data mapping package for Node.js that allows us to interact with a PostgreSQL database through our server.
To install it, run the following command:
npm install massive
Once installed, require it in your main server file:
const massive = require('massive');
Once massive is installed and required, it can be invoked to create a constant connection with your database:
massive({
connectionString: CONNECTION_STRING,
ssl: {
rejectUnauthorized: false
}
}).then((dbInstance) => {
app.set('db', dbInstance);
});
Once a connection to your database has been established, you can begin to create PostgresSQL queries in your project.
First, create a 'db' folder at the root of your project:
Inside of your 'db' folder, create a seed file.
The seed file should contain the create statements for your database schema, should you need to 'reseed' the database for any reason.
Other SQL queries can also be added and included in this folder.