/node-boilerplate

Boilerplate code for Node/express backend, Postgres database, and JWT auth

Primary LanguageJavaScript

Node/Express/Knex/Postgres/JWT Boilerplate

This repo provides a template to quickly setup a basic CRUD backend using:

Setup

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

  1. Fork and clone the repository
  2. Install dependencies: npm install
  3. Setup postgres database
  4. Modify .env file
  5. Run database migrations: npx knex migrate:latest
  6. Run seeds: npx knex seed:run
  7. Run server: npm run server

Run Tests

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

Setup PostgreSQL (Mac)

To install postgres, follow these instructions.

Create development and testing databases

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

.env Variables

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}

Documentation

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.

TO DO

  • Add migrations and seeds