serverless-sub-routing
v2.4.0
Published
Serverless Sub Routing with DI
Downloads
2
Maintainers
Readme
Serverless Sub Routing POC
This project is a Node.js application utilising routing-controllers for route management, a Service Layer for business logic, and a Repository Pattern for database interactions, specifically with DynamoDB.
serverless-sub-routing/
│
├── src/
│ ├── domain/ # Domain-specific logic, helpers, enums & models
│ │ ├── models/
│ │ ├── enums/
│ │ └── helpers/
│ │
│ ├── services/ # Business logic implementation
│ │ └── UserService.ts
│ │
│ ├── providers/ # Data source interaction layer
│ │ └── UserProvider.ts
│ │
│ ├── proxy/ # Application entry point
│ │ └── index.ts
│ │ ├── resources/ # HTTP routing handlers
│ │ │ └── UserResource.ts
│ │ └── middleware/ # HTTP route, request & response error handling
│ │ ├── BeforeMiddleware.ts
│ │ ├── CustomErrorMiddleware.ts
│ │ └── NotFoundMiddleware.ts
│ │
│ └── functions/ # Lambda functions and/or tasks that are not proxied
│ └── getUser/
│ └── handler.ts
│
├── package.json
├── tsconfig.json
└── README.md
Prerequisites
- Node.js
- npm
Getting started
npm i
- Installs dependenciesnpm run setup
- Installs Serverless DynamoDB localnpm run start
- Starts Serverless offline
npm run package
- Packages the project and outputs to /artifacts/*.zip
Architecture
Dependency Injection
We use Dependency Injection (DI) for managing dependencies between classes, making the application modular and testable.
Using an API Gateway Proxy
Resources
Resources in src/proxy/resources
handle incoming HTTP requests and delegate complex business logic to the services. They are responsible for responding to the client.
Services
The src/proxy/services
directory contains service classes. Services encapsulate the core business logic of the application. They interact with the repository layer to fetch and manipulate data.
Providers
Providers in src/proxy/providers
are responsible for direct database interactions. They abstract the data layer, making it easier to manage data operations.
Middleware
Middleware in src/proxy/middleware
is used to handle errors and other request/response operations. They are useful to hooking into parts of the lambda invocation.
Lambda Functions
Functions not using an API Gateway Proxy are in src/functions
. They are responsible for handling the event and context passed to the lambda function.
Tests
Unit Tests
Tests are written using Jest and can be found in the tests
directory.
The unit/
folder should mimic the src/
directory.
Code style
- This project uses husky to run a series of checks on
commit-msg
,pre-commit
andpre-push
- This project uses
lint-staged
andeslint
to enforce code styles - Conventional commits rules are enforced using
@commitlint/cli
- To bypass 'empty' commits, this format can be used
git commit --allow-empty -m "fix: here is some work"