0.0.1 ā€¢ Published 2 years ago

pern-boilerplate-backend v0.0.1

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

PERN Boilerplate Backend

PERN Header

A PERN (Postgres, Express, React, Node) Boilerplate for developing and go-prod

Technologies

  • šŸ–„ļø Node
  • šŸ–¼ļø React
  • ā›“ļø Typescript
  • šŸ˜ PostgreSQL

Platforms

PlatformHosted OnGithub Actions
šŸ–„ļø BackendHeroku App Statusworkflow badge
šŸ–¼ļø Webapp/App/PWA - Client SideNetlify Statusworkflow badge
šŸŽ›ļø Admin - Admin SideNetlify Statusworkflow badge

Features

  • šŸ–¼ļø Basic Frontend Theme
  • šŸ§‘ā€šŸ¤ā€šŸ§‘ User Management System
  • šŸ¤“ User Roles & Permissions
  • šŸ“± Multiplatform
  • āž”ļø Authentication through Refresh Token & Cookies
  • šŸ“˜ Email Login & Social Login (Google and Facebook)
  • šŸ’„ Logs System and Logs Rotation
  • āš–ļø GDPR Compliant

Get Started

The Get Started is quite long, but it's easy.

1. Clone Repository and Install

The first thing to do is to clone the repository and install the packages

šŸ’” Be sure to have Git and Node installed

git clone https://github.com/dfmihce/PERN-Docker-Boilerplate-Backend.git
cd PERN-Docker-Boilerplate-Backend
npm install

2. Setup the Database

The database could be created natively on your machine, or through a cloud platform such as Elephant SQL or locally through Docker.

Specifically, if it has been chosen the third way, there is a docker.compose.db.html in dockerServices/db. If you want to run the Database through this way, type the following commands from the root folder:

cd dockerServices/db
docker-compose -f docker-compose.db.yml up

3. Setup Environement Variables for Production and Development

In order to develop and deploy the Repository, it is needed to create ENV files with ENV Variables. To do so, create in the root folder (the folder in which there is the index.ts file ) 2 files: .env.developent and .env.production.

Write in the files the following environement variables.

#Server
CLUSTER=
PORT=
NODE_ENV=

#Secrets
ACCESS_TOKEN_SECRET=
REFRESH_TOKEN_SECRET=

#Access Token
ACCESS_TOKEN_EXPIRATION=

#Logs
LOG_FILE=
LOG_LEVEL=
LOG_DIRECTORY=

#Urls
ADMIN_FRONTEND_URL=
WEBAPP_FRONTEND_URL=
BACKEND_URL=

#Database
DB_USER=
DB_PASSWORD=
DB_NAME=
DB_HOST=

#Mail
MAILER_USER=
MAILER_PASSWORD=
MAILER_HOST=

#Firebase
FIREBASE_TYPE=
FIREBASE_PROJECT_ID=
FIREBASE_PRIVATE_KEY_ID=
FIREBASE_PRIVATE_KEY=
FIREBASE_CLIENT_EMAIL=
FIREBASE_CLIENT_ID=
FIREBASE_AUTH_URI=
FIREBASE_TOKEN_URI=
FIREBASE_AUTH_PROVIDER_X509_CERT_URL=
FIREBASE_CLIENT_X509_CERT_URL=
FIREBASE_DATABASE_URL=

#FACEBOOK LOGIN
FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=

#GOOGLE LOGIN
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=

In the following part, it will be explained how to compile each variable.

Clustering and Environment

CLUSTER= needs to be set false if you don't want to use the cluster mode. true, if you want to use it.

PORT= refers to the port on which the express server will listen. If not set, the default port will be set to 8000

NODE_ENV= should be set production in .env.production, while should be set development in .env.development

Secrets for Authentication

ACCESS_TOKEN_SECRET= refers to the secret key for the access token of the authentication

REFRESH_TOKEN_SECRET= refers to the secret key for the refresh token of the authentication

ACCESS_TOKEN_EXPIRATION= is the amount of days set for the expiration fo the access token (i.e. 1, 2, 3)

Logs Settings

LOG_FILE= the name of the file to store the logs (i.e. logs.log)

LOG_LEVEL= the log level according to the Winston Package (i.e. error, warn, info, verbose, debug, silly)

LOG_DIRECTORY= the directory path to store the log file (it can be either relative to the main folder such as ./src/logs or absolute)

URL Settings

ADMIN_FRONTEND_URL= the url of the frontend of the admin side

WEBAPP_FRONTEND_URL= the url of the frontend of the app side

BACKEND_URL= the url of the backend

Database Settings

DB_USER= the database user that will be used to connect to the database DB_PASSWORD= the database password that will be used to connect to the database DB_NAME= the database name DB_HOST= the database host

Mail Settings

MAILER_USER= MAILER_PASSWORD= MAILER_HOST=

The settings of the mailer from your mail provider

Social Logins

FACEBOOK_CLIENT_ID= FACEBOOK_CLIENT_SECRET=

GOOGLE_CLIENT_ID= GOOGLE_CLIENT_SECRET=

These settings are received after the configuration of the Google and Facebook Login through Google Login and Facebook Login

4. Setup certificates for Development

In order to develop, certificates are needed and need to be stored in the server/keys folder. They need to have the following names: privatekey.pem, certificate.pem

It's suggested to create self signed certificated for development using these commands:

5. Configs

In ` Link 1 , While using the comments, add '-config <folderOf_openssl.cnf> ' _openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

Link 2

6. Deploy

It's possible to deploy the application using the following ways: with node, with docker or with PM2. in the package.json are defined the following commands:

"deployWithPM2": "env-cmd -f .env.production pm2 start ecosystem.config.js"

"deployWithNode": "env-cmd -f .env.production ts-node ./build/index.js"

"deployOnDocker": "env-cmd -f .env.production ts-node ./build/index.js"

prebuild and postbuild are used to run the commands before and after the build and are triggered automatically

7. Development

In order to develop, the .env.development file is needed. In order to set up a development database, create a container using the docker-compose.db.yml in the dockerServices folder. lastly, run the following command:

npm run dev

The localhost should be available at https://localhost:PORT where PORT is the port defined in the .env.development file.