This repo provides a template to quickly setup a basic CRUD backend using:
- Node/Express
- PostgreSQL relational database
- Knex SQL query builder
- JWT for authentication
- Jest/Supertest for testing
- apidoc for documentation
To use this boilerplate, please follow the steps below:
NOTE: Skip steps 5 and 6 for now. Migrations and seeds have not been run yet and are on my todo list
- Fork and clone the repository
- Install dependencies:
npm install
- Setup postgres database
- Modify
.env
file - Run database migrations:
npx knex migrate:latest
- Run seeds:
npx knex seed:run
- Run server:
npm run server
npm run tests
Tests are set up to run continously. If you want to run them manually, remove the --watch
flag from the test
script in package.json
To install postgres, follow these instructions.
Start the Postgres CLI:
psql
If you have not previously done so, create a user with permission to create a database with the following command:
createuser {username} --createdb
Switch to your username:
psql postgres -U {username}
Create development database:
CREATE DATABASE {db-name};
Create testing database:
CREATE DATABASE {db-name-test};
Exit
\q
Create a .env
file at the root of your project and add the following for development and testing databases:
POSTGRES_DEV_HOST=localhost
POSTGRES_DEV_PORT=5432
POSTGRES_DEV_USER={postgres username}
POSTGRES_DEV_PASSWORD={postgres password, if you set one}
POSTGRES_DEV_DATABASE={db-name}
POSTGRES_TEST_HOST=localhost
POSTGRES_TEST_PORT=5432
POSTGRES_TEST_USER={postgres username}
POSTGRES_TEST_PASSWORD={postgres password, if you set one}
POSTGRES_TEST_DATABASE={db-name-test}
This project uses apiDoc to generate in-line documentation. It keeps your documentation close to your code for easy maintainability (and so you never forget to update it!).
Basic documentation is already written up for the users endpoint, and the generated documentation can be found in the docs
folder.
To view the docs, open the ./docs/index.html
file in your browser.
To update the index.html
file after you have made changes to your documentation, run:
npm docs
You can read more about the different parameters you can use with apiDoc on their documentation page.
If you prefer to use another API documentation system, you can remove the @apidoc
code in the api routers files.
- Add migrations and seeds