This project attempts to provide a simple framework for API development using express with the freedom of selecting and implementing any database.
This app implements a small retail store wherein store administrators can create users (customers), products and post a sale transaction. Each sale transaction record has an active user (customer) and active product.
Core idea is to provide service definitions which conform to business logic as interfaces under src/service/ (service definitions). Service definitions may or may not be dependent on a persistent storage (database / external APIs / files) but if it they are, then persistent storage is implemented by implementing corresponding interface at src/repository/ (service repository). Similarly, business logic of a particular service definition is defined at src/controller/ (service controller). If persistent storage is required then service controller depends upon a service repository which implements the relevant service definition. Express APIs then directly use service controller objects.
For rest of things such as DB implementation, Swagger, etc. refer Project structure
┌---------------------------┐
| Service Definition (SD) |
| |
| User |
| - getDetail() |
| |
┌-----------┐ ┌--------------------------┐ | FileHandler |
| C | | API | | - upload() |
| L | | Depends on SC | | - download() |
| I | | | ┌------| |------┐
| E |◄---| - /upload | | | Authenticate | |
| N | | └FM.uploadAuth() | | | - authUser() | |
| T | | | | | | |
| | | - /download | | | FileManager | |
| Calls API | | └FM.downloadAuth() | | | - uploadAuth() | |
└-----------┘ └--------------------------┘ | | - downloadAuth() | |
▲ | | | |
| | └---------------------------┘ |
| ▼ ▼
┌-------------------------------------------┐ ┌-------------------------------------------┐
| Service Controller (SC) | | Service Repository (SR) |
| Implements SD, may depend on SR | | Implements SD, connects to other API, DB |
| | | |
| Authenticate (AUTH) |◄-----------| User (USR) |
| - authUser() | | - getDetail() |
| └USR.getDetail() | | └Query Database |
| | | |
| FileManager (FM) | | FileHandler (FH) |
| - uploadAuth() | | - upload() |
| └AUTH.authUser() | | └Upload file to server |
| └FH.upload() | | |
| | | - download() |
| - downloadAuth() | | └Download file from server |
| └AUTH.authUser() | └-------------------------------------------┘
| └FH.download() |
└-------------------------------------------┘
Folder Path | Description |
---|---|
env/ | Environment files |
logs/ | Logs directory |
src/controller/ | Business Logic implementation of services defined in src/sevice/ |
src/core/ | Basic functionalities required across the project |
src/core/repository/ | Database implementation. Currently supports MongoDB, MySQL and LokiJS |
src/di/ | Dependency injection configuration. Implemented using inversify |
src/entity/ | Class and Interface definitions |
src/error-handler/ | Express error handler with custom error objects |
src/event/ | Implement various event streams primarily for logging |
src/middleware/ | Routers and Request handlers for managing basic express security, authentication and authorization |
src/repository/ | Repository implementation of services defined in src/sevice/ |
src/routes/ | Express API definitions |
src/service/ | Service definitions |
src/swagger/ | Swagger 2.0 definition and API router |
src/index | Handles server initialization and startup |
test/ | Test scripts |
test/api/ | API tests |
test/di/ | Dependency injection configuration for controller and repository tests |
test/controller/ | Controller tests |
test/repository/ | Repository tests |
test/rest/ | Tests using REST client. Not required. Not executed during npm test . |
util/build.js | Utility to build project |
nodemon.json | Nodemon configuration |
package.json | Project dependencies |
tsconfig.json | Transpiling configuration |
tsconfig.prod.json | Production transpiling configuration |
tslint.json | TS Linting rules |
Clone this repository and go to this project's root location
npm i -g tsc ts-node ts-mocha tslint nodemon
npm i -D
Refer DB configuration file
npm run start:dev
Open browser and go to http://localhost:3000/api-docs
npm test
npm run build
npm start