DEV Community

Cover image for API Template using Clean Node Architecture
Wai Phyo Naing
Wai Phyo Naing

Posted on • Updated on

API Template using Clean Node Architecture

Getting Started

I am introduced a simple API template for backend developers using clean architecture based on express application. This article is meant to help you with improved code.

Main features

The template supports admin dashboard, access menu permission based on role, switch custom theme, secure data with authN and authZ, integrate with chart.js lib, generate easily CRUD API, upload media files, cache with redis memory, switch multi databases and design for TDD.

The followings must be pre-installed on your machine:

Node.js, MongoDB, MySQL, Redis

Clone itemplate repository

git clone https://github.com/waiphyo285/itemplate-backend.git
Enter fullscreen mode Exit fullscreen mode

Navigate root directory and install dependencies

npm install
Enter fullscreen mode Exit fullscreen mode

Migrate MySQL database and tables

npx knex migrate:latest
npx knex seed:run
Enter fullscreen mode Exit fullscreen mode

Run app and then go to browser

npm run dev
localhost:6060
Enter fullscreen mode Exit fullscreen mode

TDD ./**/*.spec.js in controllers

npm run test
Enter fullscreen mode Exit fullscreen mode

CLI commands in src/cli

node index
node index --index
node index --show=623210497fc2cb28840d1448
Enter fullscreen mode Exit fullscreen mode

Clean Architecture

First of all, let me admit this article is inspired to develop this project. Uncle Bob's famous Clean Architecture is a way to write resilient software.

Resilient software is divided into layers, underpinned by business logic and is independent of technologies. It should be:

  1. Testable: Can be tested without external dependencies.
  2. Independent of UI: You can easily switch CLI for Web or RasberryPi.
  3. Independent of Database: Switch out SQL [MySQL or PostgreSQL] for MongoDB.
  4. Independent of Frameworks: Libraries and frameworks should be treated as tools and not dependencies.
  5. Independent of any external agency: Business rules don't know anything about outside world.

In practice, choice of technology should be the last decision you make or code you write (e.g. database, platform, framework). By following clean architecture, you can write software today that can be easily switched out for different technologies in the future.

Click here for github repo.

_Note: this application is different to the Clean Architecture diagram above but attempts to achieve the same outcome.

Oldest comments (0)