Skip to content

Commit

Permalink
initial commit, taken from courses
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinwilta committed Apr 30, 2022
1 parent 4838650 commit 536a88a
Show file tree
Hide file tree
Showing 29 changed files with 8,312 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ENV=DEV

MONGO_LOCAL_HOSTNAME=
MONGO_LOCAL_PORT=
MONGO_LOCAL_DB=

MONGO_USERNAME=
MONGO_PASSWORD=
MONGO_CLUSTER=
MONGO_DB=

MONGO_TEST_HOSTNAME=
MONGO_TEST_PORT=
MONGO_TEST_DB=

SERVER_HOSTNAME=
SERVER_PORT=

SECRET=
REFRESH_SECRET=

SERVICE=
EMAIL=
PASSWORD=
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
node_modules/
.env
65 changes: 63 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,63 @@
# ts-express-boilerplate
Boilerplate for Express and Typescript
# Typescript Boilerplate with MongoDB

to run server with dev settings use `npm run start:dev`

build server with `npm run build`

start server with `npm start`

start test with `npm run test`

# Notes:

## Mongoose Middleware vs Express Middleware

Mongoose middleware:
`schema.pre()`

- Hooks, to apply multiple database queries
- DO NOT use arrow functions

Express middleware:
`app.use()`

- Standard middleware, to procces the req/res data

## Layers in the server

> Italics are optional
[HTTP Endpoint] ⇄ _[Middleware]_ ⇄ [Controller] ⇄ [Service] ⇄ _[Mongoose Hooks]_ ⇄ [Database]

Functionalities of each folder:
Folder Name | Description | Functionalities
------------|-------------|----------------
config | Constants for server configuration | declaring database url, environment, etc
constants | Reusable constants | Declaring constants that'll be used repeatedly
middlewares | Express middlewares | req/res preprocessing(?), e.g. jwt/cookie authentication, session validation, etc
routes | All API routes | All routes and their respective middleware and callbacks
controller | Layer between _HTTP Request_ (or _middleware_) and services | Interface for handling req/res before sending it to worker (services)
services | API worker | All necessary operations before/after querying to database, make sure to THROW any errors, logging are optional whether you want to log it on controller or service for better debugging
interfaces | Typing for mongoose schema | Interface for typescript's typing to mongoose schemas
utils | functions to support services | All functions that can be separated from services to make it modular
models | field attribute of database schema | All hooks and schema structure will be defined here, e.g. whether if a field is required or not, reference to other models, etc

## Tips to create an endpoint

1. Create interface
> It is recommended to match interface with database attributes to provide consistency of data type, add more interface to improve consistency between data types
1. Create services
> a. Create services according to what the worker will do, it is recommended to create workers with single responsibility and reuse them later on
> b. Handle missing parameter and parameter types, you can use interface to simplify this process
2. Create models
> Create schema model accordingly, use Object id if it needs some kind of "relationship" between collection
3. Create controller
> a. Handle request parameters and error handling (req/res)
> b. If possible, do not pass whole request body to services, it is better to validate each of the request and pass them to services accordingly
4. Create route
> Define "parent" route on index.ts and define its subroute on another file
5. (opsional) Create middleware
> Call middlewares before controller (in routes)
6. Test API

- Don't forget to use logging and import logging from utils. If necessary, you can rename the logger to something else to avoid conflict with autocomplete (there are other "Logger")
Loading

0 comments on commit 536a88a

Please sign in to comment.