Project Setup
In light of our requirements, we know that we will need to cater for at least two data structures - Reader and Book, with the corresponding controllers, routes and tests.
Getting set up
We are going to set up an express
app which uses Sequelize
to interact with a Postgres
database. The steps assume you have Postgres
running in a docker
container on your machine. It also references the music-library-api
. If you get stuck, it would be a good idea to check back there for examples.
We will start by setting up the repository, a basic express app and a mocha/chai/supertest test suite.
Repository Setup
-
Create a new directory for the project on your computer
-
Create a new
README.md
file in the root of your project -
Initialise a git repo in the new directory with
git init
-
Add an empty
.gitignore
file -
Create a repository for the project on Github
-
Connect your local repository to the remote GitHub repository
-
Initialise a new
NPM
project in your project directory withnpm init
1 Add node_modules
to your .gitignore
file
Application Setup
-
Install
express
, andpg
as dependencies -
Set up your basic
index.js
andsrc/app.js
similar to how you did in theMusic API
projectsrc/app.js
should configure and export a basic Express application.index.js
should callapp.listen()
and thenconsole.log()
the port that the app is listening on.
-
Install
dotenv
andnodemon
as development dependencies -
Add a
start
script to yourpackage.json
file with the following command:nodemon -r dotenv/config index.js
. This usesnodemon
to execute theindex.js
file with the environment variables loaded from a.env
file. -
Create a scripts folder, and copy the
create-database.js
anddrop-database.js
scripts from themusic-library
. Yourcreate database
script only needs to create the database. We will useSequelize
to handle setting up our tables. -
Add a
prestart
script to yourpackage.json
. Set the command tonode scripts/create-database.js
. This will run before thenpm start
command, and will create your database if it doesn't already exist. -
Create a
.env
file with the following variables:
At this point you should be able to run npm start
in your terminal, and get a listening express server.
Test Environment Setup
-
Install
mocha
,chai
, andsupertest
as dev dependencies. -
Copy across the
test-setup.js
from themusic-library-api
-
Add a
.env.test
with the same environment variables as your.env
. Make sure to give your test database a different name. -
Add
.env.test
to your.gitignore
-
Add a
test
script to yourpackage.json
file:mocha tests/**/*.js --exit --recursive --timeout 60000 --file ./tests/test-setup.js
-
Add a
pretest
script to yourpackage.json
. Set the command to:node scripts/create-database.js test
. Note that this time we pass thetest
option at the end of the command. This tells the script to load the variables from.env.test
instead of.env
. -
Add a
posttest
script, set the command to:node scripts/drop-database.js test
. This will delete your test database after your tests have finished running.
At this point, you should be able to run npm test
in your terminal. Mocha should run and inform you that there are no tests yet. Commit your work to Github.