Skip to content

akash-kansara/express-typescript-boilerplate

Repository files navigation

Express TypeScript Boilerplate

GitHub version Dependency Status devDependency Status Build Status Coverage Status

This project attempts to provide a simple framework for API development using express with the freedom of selecting and implementing any database.

Application Business logic

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.

Project Structure logic

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

Pictorial representation for a file management service with user authentication:

                                                            ┌---------------------------┐
                                                            |  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()               |
                        └-------------------------------------------┘

Project structure

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

To start server, follow these steps

Step 1: Install NodeJS & NPM

NodeJS & NPM

Step 2: Get project

Clone this repository and go to this project's root location

Step 3: Add global dependencies

npm i -g tsc ts-node ts-mocha tslint nodemon

Step 4: Add local dependencies

npm i -D

Step 5: Configure Database

Refer DB configuration file

Step 6: Run in dev environment

npm run start:dev

Step 7: Check API

Open browser and go to http://localhost:3000/api-docs

Step 8 (Optional): Run test cases

npm test

Step 9: Build for production

npm run build

Step 10: Run in production environment

npm start