Node API boilerplate by nave.rs
A boilerplate for building RESTful APIs using Node.js, PostgreSQL, koa, knex, objection.
Getting started
Installation
- Install Node.JS LTS version
- Install PostgreSQL
- Clone this repository and enter on the respective folder
- Install dependencies running:
yarn
ornpm install
Things to do before run the project:
- Create database (by follow the commands):
- CREATE USER
user
WITH PASSWORDpassword
- CREATE DATABASE
database
- GRANT ALL PRIVILEGES ON DATABASE
database
touser
-
Install
citext
extension on your database with the commandCREATE EXTENSION citext;
-
Change name value of .env.example to .env and set the key SECRET to any value you wish
-
Change DATABASE_URL to postgress://
user
:password
@localhost
/database
-
Run migrations:
yarn db:migrate
-
Run seeds:
yarn db:seed
-
Install Docker
- Pull redis:
docker pull redis
- Create image:
docker run --name your-image-name -d redis
- Start redis:
docker start your-image-name
- Run
yarn start
oryarn dev
to start server
Testing
- Run tests:
yarn test
Directory Structure
├── /src
| ├── /controllers
| ├── /database
| | ├── /migrations
| | ├── /seeds
| ├── /helpers
| ├── /middleware
| ├── /models
| ├── /routes
| ├── /validators
├── /test
Postman
Styleguide
Nodemailer
To use the nodemailer helper in a non-production environment, you need to set the variable in .env ALLOW_LIST
and assign an array with the domains allowed for sending e-mail. This domain must start with an @
, followed by a minimum of 2 characters, a .
and 2 characters in the end.
In case the informed email is not part of any informed domain, the email will not be sent.
Examples:
- ALLOW_LIST=["@nave.rs"]
- EMAIL_1: [email protected] -
ALLOWED
- EMAIL_2: [email protected] -
NOT ALLOWED
The following domains: nave.rs
and@nave
are not allowed as they do not satisfy the domain description, given above.
Database
This project uses objection.js as ORM and knex as query builder.
Migrations:
In order not to use before update hooks on our models, we decided to create a function and trigger via migration, everytime you create a table with updated_at add the trigger to it. (an example is in the file: "src/database/migrations/20180910221504_init.js")
Steps to use Queue UI
- After adding a new queue, need to add in queue.js a new
adapter
- Acess UI on http://localhost:3001/v1/queues/dashboards