Piece of my thoughts about Node.js architecture.
supra-api-nodejs: A little bit about Node.js RESTful APIs Architecture (RU)
Supra-api-nodejs its about monolith first approach. But this does not prevent you from using it in a microservice architecture as well.
Each entity have own controller class it slim layer representing resource mapping(routing)
class PostsController extends BaseController {
static get router () {
router.get('/', this.actionRunner(actions.ListAction))
router.get('/:id', this.actionRunner(actions.GetByIdAction))
router.post('/', this.actionRunner(actions.CreateAction))
router.patch('/:id', this.actionRunner(actions.UpdateAction))
router.delete('/:id', this.actionRunner(actions.RemoveAction))
return router
}
}
For example PostsController
implements post
entity routes. Each route fires own action
.
It's a class encapsulated request validation, permission verification and business logic. One file, one class, one REST operation, one use case.
Implement data access methods.
Represent models schemas and validation rules. There is no other logic only model fields and validation rules.
npm i -g knex nodemon
cd /supra-api-nodejs
cp .env.example .env
.env
Run migration to set base SQL schema
knex migrate:latest
Run server
npm run start // prod mode
npm run dev // dev mode
Path | Method | Description |
---|---|---|
/auth/login | POST | LoginAction |
/auth/logout | POST | LogoutAction |
/auth/refresh-tokens | POST | RefreshTokensAction |
Path | Method | Description |
---|---|---|
/users | GET | ListUsersAction |
/users/current | GET | GetCurrentUserAction |
/users/:id | GET | GetUserByIdAction |
/users | POST | CreateUserAction |
/users | PATCH | UpdateUserAction |
/users/:id | DELETE | RemoveUserAction |
/users/change-password | POST | ChangePasswordAction |
/users/send-reset-password-email | POST | SendResetPasswordEmailAction |
/users/reset-password | POST | ResetPasswordAction |
/users/confirm-registration | POST | ConfirmRegistrationAction |
/users/change-email | POST | ChangeEmailAction |
/users/confirm-email | POST | ConfirmEmailAction |
/users/resend-confirm-new-email-token | POST | ResendConfirmNewEmailTokenAction |
/users/cancel-email-changing | POST | CancelEmailChangingAction |
Path | Method | Description |
---|---|---|
/posts | GET | ListPostsAction |
/posts | POST | CreatePostAction |
/posts/:id | GET | GetPostByIdAction |
/posts/:id | PATCH | UpdatePostAction |
/posts/:id | DELETE | RemovePostAction |
!!! Project still in progress !!!
2017 - 2018 - 2019 - 2020 ...