This repo contains a basic Node and Express app to get you started in constructing an API. To get started, clone this repo and run yarn
or npm install
in your terminal at the project root.
Both the development database(shopping) and test database(shopping_test) are in psql
. If your system does not have Postgres installed, please follow the installation instructions here.
After downloading and installing postgres, On one of the terminal windows , follow the following steps :
-
For Linux based :
- switch to the postgres user
su postgres
. - start psql
psql postgres
- switch to the postgres user
-
For Windows:
- simply open
psql
shell and connect to the any existing database (if no databases exist connect to pre existingtemplate0
ortemplate1
)
- simply open
-
In psql run the following:
- CREATE USER shopping_user WITH PASSWORD 'password123'; Create dev database
- CREATE DATABASE shopping;
- \c shopping
- GRANT ALL PRIVILEGES ON DATABASE shopping TO shopping_user;
- Test that it is working run \dt and it should output "No relations found." Create test database
- CREATE DATABASE shopping_test;
- \c shopping_test
- GRANT ALL PRIVILEGES ON DATABASE shopping_test TO shopping_user;
- Test that it is working run \dt and it should output "No relations found."
-
Make sure to use environment variables in a
.env
file to connect to the Db based on the mode of operation i.edev
ortest
which is also stored as an enviromental variableENV
. Thedatabase.ts
file insidesrc
folder is responsible for connecting to the relevant DB. -
On another terminal window :
-
install yarn :
npm install yarn -g
-
install db-migrate on the machine for terminal commands
npm install db-migrate -g
-
check node version node -v - it needs to be 10 or 12 level
-
IF node was not 10 or 12 level, run (only valid for Linux based systems)
npm install -g n n 10.18.0 PATH="$PATH"
For windows systems, use
nvm
Link and instructions here -
To check that the version is 10 or 12 -
node -v
-
install all project dependencies
yarn
ornpm install
-
to run the migrations:
- When in
dev
environment, rundb-migrate up
to run the migrations on development databaseshopping
. - Make sure to change the
ENV
in.env
file totest
before running the following command . When intest
environment, runyarn test
to run the tests . This is an npm script in thepackage.json
file which runs the up migration, the jasmine test suite on both models and routes and upon successful testing runs the down migrations to remove all tables from test databaseshopping_test
. to test that it is working, run yarn watch should show an app starting on 0.0.0.0:3000
- When in
-
The application uses the following libraries:
- Postgres for the database
- Node/Express for the application logic
- dotenv from npm for managing environment variables
- db-migrate from npm for migrations
- jsonwebtoken from npm for working with JWTs
- jasmine from npm for testing
- Migrations contained within the
migrations
directory. Also contains thetest
migration folder to enable db migrate scoping for test database. src/handlers
contains all the routes of the application. Also, contains the token verification middleware which is required on certain routes.src/models
contains the Db facing methods to allow the application to interact with the DB.tests
contains the jasmine test suite on both the routes (routes.spec.ts
) and models(models.spec.ts
).Please note that therandom
test feature of jasmine has been set tofalse
for testing this project.Test the models and routes separately by commenting out tests in one file to test the code in the other file and vice versa.- JWT has been implemented within the project to allow only authorised users to access certain routes. Also , the passwords are stored in a hashed format using
bcrypt
within the db. ❌ No plain text passwords ❌ - The Express application is CORS enabled ✔✔
- The link contains the API documentation for the application.API Documentation
- Please note that the repo also contains a json file
Storefront API.postman_test_run.json
which has data related to a successful run of the API on Postman Client.