RESTful Code Structure

notes 1 min #Rest#Code#Code-Structure

Good practices is to organise code to separate concerns.

For an API there are three layers

  1. Web layer
  2. Service layer
  3. Data access layer

Web layer

The web layer is where we process HTTP requests, interface with the service layer and return a http response.

The main abstractions utilised in the web layer include:

Routes

Route endpoints should be separated into entities which are then broken down into actions.

  1. Get
  2. Get one
  3. Post
  4. Delete

Controllers

Controllers should be unpacking the HTTP request such as headers, URL parameters, body data etc. before sending actions to the services layer

Middleware

Middleware hold functions that are common across routes.

Service Layer

The service layer is where we apply the business logic and make requests of the data layer.

Data Layer

This is where we communicate with the database.

References