|
| 1 | +# Node Express API Boilerplate |
| 2 | +This project is based on the |
| 3 | +[Node API and Client Boilerplate](https://github.com/anthub-services/node-api-and-client-boilerplate). |
| 4 | +Required node version is `9.2.1`. |
| 5 | +The API app is powered by [Express](https://expressjs.com/) |
| 6 | +and used as API service for the [Create React App Boilerplate](https://github.com/anthub-services/create-react-app-boilerplate). |
| 7 | + |
| 8 | +## Other API App Boilerplate |
| 9 | + |
| 10 | +- [Node Express API Mockup Data Boilerplate](https://github.com/anthub-services/node-express-api-mockup-data-boilerplate) – |
| 11 | + non-database API server powered by [Express](https://expressjs.com/) |
| 12 | + |
| 13 | +## Starting the App |
| 14 | + |
| 15 | +Note: Only change the environment variables for `POSTGRES_USER` and `POSTGRES_PASSWORD` if working on local machine. |
| 16 | + |
| 17 | +Copy `.env.dist` to `.env` and change the values of the environment variables if needed. |
| 18 | + |
| 19 | +``` |
| 20 | +PORT=7770 |
| 21 | +ALLOW_ORIGIN=http://localhost:7771 |
| 22 | +JWT_SECRET=jwtsecretcode |
| 23 | +POSTGRES_PORT=5432 |
| 24 | +POSTGRES_DB=express_api_dev |
| 25 | +POSTGRES_USER=express_api_user |
| 26 | +POSTGRES_PASSWORD=root |
| 27 | +``` |
| 28 | + |
| 29 | +Then run the following commands: |
| 30 | + |
| 31 | +``` |
| 32 | +yarn |
| 33 | +yarn start |
| 34 | +``` |
| 35 | + |
| 36 | +Note: The database must be created and initialized after starting the app on fresh installation. |
| 37 | +See **Database using PostgreSQL and Sequelize** section. See **Bash Commands** section for Docker. |
| 38 | + |
| 39 | +Access the app at <http://localhost:7770>. |
| 40 | + |
| 41 | +## Docker |
| 42 | + |
| 43 | +Download and install the [Docker Community Edition](https://www.docker.com/community-edition). |
| 44 | + |
| 45 | +Note: See **Bash Commands** section for Docker. |
| 46 | + |
| 47 | +## Database using PostgreSQL and Sequelize |
| 48 | + |
| 49 | +**Installing PostgreSQL** |
| 50 | + |
| 51 | +**Mac:** [Getting Started with PostgreSQL on Mac OSX](https://www.codementor.io/engineerapart/getting-started-with-postgresql-on-mac-osx-are8jcopb) |
| 52 | +<br> |
| 53 | +**Windows:** [Installing PostgreSQL, Creating a User, and Creating a Database](https://confluence.atlassian.com/display/CONF30/Database+Setup+for+PostgreSQL+on+Windows) |
| 54 | + |
| 55 | +NOTE: For Mac users, you can run the PostgreSQL server on a separate terminal console by running the following command: |
| 56 | + |
| 57 | +``` |
| 58 | +postgres -D /usr/local/var/postgres |
| 59 | +``` |
| 60 | + |
| 61 | +**Create and Initialize Database** |
| 62 | + |
| 63 | +Open a terminal console and run the following commands: |
| 64 | + |
| 65 | +``` |
| 66 | +sequelize db:create |
| 67 | +sequelize db:migrate |
| 68 | +sequelize db:seed:all |
| 69 | +``` |
| 70 | + |
| 71 | +To drop database, run the following command: |
| 72 | + |
| 73 | +``` |
| 74 | +sequelize db:drop |
| 75 | +``` |
| 76 | + |
| 77 | +See **Bash Commands** section for Docker. |
| 78 | + |
| 79 | +## Bash Commands |
| 80 | + |
| 81 | +On the `root` directory of the project, run the following commands: |
| 82 | + |
| 83 | +Note: To view the Docker containers, open another terminal console then enter `docker ps`. |
| 84 | +To manage separate Docker instance for API, open another terminal console and run the commands below. |
| 85 | + |
| 86 | +### Docker |
| 87 | + |
| 88 | +| Command | Description | |
| 89 | +|----------------------------------------|-----------------------------------------------------| |
| 90 | +| `./bin/install` | Build the Docker containers and initialise database | |
| 91 | +| `./bin/reinstall` | Re-build containers and re-initialise database | |
| 92 | +| `./bin/start` | Start all the services (API and database) | |
| 93 | +| `./bin/stop` | Stop all the services | |
| 94 | +| `./bin/console <container ID or Name>` | Access the terminal console of the API container | |
| 95 | + |
| 96 | +### Database |
| 97 | + |
| 98 | +**Local** |
| 99 | + |
| 100 | +| Command | Description | |
| 101 | +|---------------------------------------|------------------------------------------------------------| |
| 102 | +| `./bin/pg/local/start` | Start the PostgreSQL server (for Mac users only) | |
| 103 | +| `./bin/pg/local/resetdb` | Drop and re-initialise database | |
| 104 | +| `./bin/pg/local/migrate` | Run new schema migration | |
| 105 | +| `./bin/pg/local/migrateundo` | Revert the recent schema migration | |
| 106 | +| `./bin/pg/local/seed <seed file>` | Run specific data seed file with or without .js extension | |
| 107 | +| `./bin/pg/local/seedundo <seed file>` | Revert the seed of specific data seed file | |
| 108 | +| `./bin/pg/local/psql` | Access the database console | |
| 109 | + |
| 110 | +**Docker** |
| 111 | + |
| 112 | +- To run the commands for Docker database service, simply remove the `local` from the command |
| 113 | +- The `start` command works only in local machine |
| 114 | +- Used `./bin/pg/psql <database container ID or Name>` to access the database console |
| 115 | + |
| 116 | +## Users |
| 117 | + |
| 118 | +Use the following credentials to test different API responses. Default password for all accounts is `password`. |
| 119 | + |
| 120 | +- **Admin User:** `admin@email.com` - can access all applications |
| 121 | +- **Admin User witout Settings page:** `admin_no_settings@email.com` - no access on admin Settings page |
| 122 | +- **User redirected to internal page:** `referrer@email.com` – when `redirect.url` is set without the domain, |
| 123 | +user shall be redirected to internal page if no location path (referrer) found on the Sign In page |
| 124 | +- **User redirected to external page:** `redirect@email.com` – when `redirect.external` and `redirect.url` are set, |
| 125 | +user shall be redirected to external page if no location path (referrer) found on the Sign In page |
| 126 | +- **Blocked User:** `blocked@email.com` – user is signed in but the account is blocked |
| 127 | +- **Unauthorized User:** simply enter wrong `email` and/or `password` |
0 commit comments