This video is available to students only

Server setup, part 2

Connecting to a database and finalizing the API

Server setup (part 2)#

Now we will add data to the database and serve it with an API. We'll use a local database for testing, so be sure to have PostgreSQL installed on your machine and have a database created.

Adding data to the database#

Once the database is created, create the sales table and insert the sample data by running the provided sales.sql file.

Click here to download the sales.sql file.

You can add data to the database using the command line or by using a database client like pgAdmin.

The database connection string will be stored in an environment variable called DATABASE_URL, which Heroku has already set up for us on the remote server. We must do the same locally with a .env file in the root myapp directory. (The dotenv package allows us to do this.) We can also set up a DEV_MODE environment variable to distinguish between local and remote deployments within the code itself.

Adding the API endpoint#

Now we'll add the API endpoint. First we'll import a few more dependencies into index.js.

Β 

Start a new discussion. All notification go to the author.