Backend of the difabel project. This project is using the project template from BNCC@Bandung RnD division.
- Make sure you have installed yarn and PostgreSQL.
- Clone the repo
git clone https://github.com/Hydra-Gang/difable-project-be.git
- Install the dependencies
yarn install
- Duplicate the
.env.example
file to.env
and fill the database credentials - Generate JWT secrets
yarn jwt:generate
- (Optional) If you want, you can seed data
yarn seed
- Run the dev server
yarn auto
<your project>\
|--scripts\ # User scripts for automating
|--src\ # Source folder
|--configs\ # Application configs
|--controllers\ # Route controllers
|--decorators\ # Custom decorators
|--entities\ # Database models/entities (represents table)
|--middlewares\ # Custom middlewares
|--routes\ # Server routes, provides automatic routing
|--typings\ # Custom types/interface for type assertion
|--utils\ # Utility classes and functions
|--api.util.ts # Server response utility
|--validations\ # Schemas for validating JSON
|--app.ts # Express app and it's configuration
|--ormconfig.ts # TypeORM config
|--server.ts # Program entry point (db connection is also here)
|--.eslintrc.json # ESLint config
|--tsconfig.json # TypeScript compiler config
|--...
Running:
# compiles the project to `dist` directory
yarn compile
# diagnose the TS compiler
yarn compile:debug
# starts the program (must be compiled first)
yarn start
# automatically compiles and starts the program (not used in production)
yarn auto
Data seeding:
# Add a bunch of prepared data in `seeder.ts` file
yarn seed
Cleans the compiled files (in dist
directory):
yarn clean
Linting:
# runs ESLint to `src` directory
yarn lint
# fixes ESLint errors (for fixable errors only)
yarn lint:fix
TypeORM:
# shows TypeORM commands
yarn typeorm -h
# example: shows the migration status
yarn typeorm migration:show
JSONWebToken:
# generate JWT secrets (both access and refresh secrets)
yarn jwt:generate
Found in the .env
file
# the JWT secrets
JWT_ACCESS_SECRET=
JWT_REFRESH_SECRET=
# the postgres database credentials
DB_HOST=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
- express
- Node.js backend framework.
- Extra note,
body-parser
is already built-in to this package (ex:express.json()
) so you don't need to install it.
- cors
- Middleware to enable CORS (Cross-origin resource sharing).
- Allows the frontend devs to access the backend.
- helmet
- Secures the backend HTTP headers.
- It doesn't protect you from literally everything, but at least there's something.
- http-status-codes
- To avoid magic numbers and use constants enum, ex: using
BAD_REQUEST
instead of400
.
- To avoid magic numbers and use constants enum, ex: using
- joi
- Library for validating JSON, making it easy to make sure all (or certain) properties exists and valid.
- luxon
- Better date and time library than the default
Date
from JS. - Why not
momentjs
? It has stopped it's development, check here.
- Better date and time library than the default
- pg
- PostgreSQL database for our backend projects, although we won't be using this directly, but through
typeorm
.
- PostgreSQL database for our backend projects, although we won't be using this directly, but through
- typeorm
- ORM (Object-relational mapping) library for Node.js.
- Helps us to access the database without a need to write SQL queries.
- It can prevent typos in SQL query.
- It can make cleaner codes, thus more readable.
- It's perfect for TypeScript users.
- bcrypt
- Securing passwords easily, it hashes and also adds salt to it.
- It's a bad practice to store passwords in plain-text, this forum explains why it's bad.
- jsonwebtoken
- Token based user authentication, we need to know whether user is logged in or not.
- It's more secure when compared to Cookie and Session.