The working architecture of NodeJs applications

koorchik 2,046 views 40 slides Oct 29, 2017
Slide 1
Slide 1 of 40
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40

About This Presentation

Talk at KharkivJs 2017, Viktor Turskyi.
Why talk about the architecture?
1) 99% of NodeJs examples on Internet are “hello world” examples
2) A lot of misunderstandings around architectural patterns


Slide Content

The working architecture of
NodeJs applications
Viktor Turskyi
CTO at WebbyLab Kyiv, Ukraine

About me
●CTO at WebbyLab
●Open Source developer
(Perl, JavaScript)
●13 years in Software
development
●Successfully delivered more
than 60 projects

Why to talk about the architecture?
99% of examples in Internet - “hello world” with express/sails etc.
A lot of misunderstandings around architectural patterns

MVC (from wikipedia)

Where to place this code? Model or Controller?

Fat Stupid Ugly Controllers
“The M in MVC: Why Models are Misunderstood and Unappreciated” Pádraic
Brady
http://blog.astrumfutura.com/2008/12/the-m-in-mvc-why-models-are-misunderstoo
d-and-unappreciated/

Is Model (MVC) and Domain Model the same?

Model (from MVC)/Domain Logic
●Domain model
●Transaction script
●Table module
●Service layer

Controllers
Services
Domain model
Data access
layer
Dispatcher
Layers

The way of thinking about Controllers
●Extremely thin layer
●Protects underneath layers from everything related to HTTP
●If you change JSON to XML (or even CLI), only controllers should be rewritten

The way of thinking about Domain Model
●Belongs to Model layer of MVC
●The core part of your application
●You have almost all of your business logic here (not only database access)!!!
●Knows nothing about service layer and upper layers
●Responsible for data storing and data integrity
●Fine grained API (not suited for remote invocation)

The way of thinking about Services
●Belongs to Model layer of MVC
●Contains application logic
●Does not trust any incoming params
●You should keep thin if possible
●Knows nothing about controllers/transport/UI.
●Use cases based API
●Knows about context (what user asks for data)
●Knows when and how to notify user (emails etc)
●Does coordination and security
●Coarse grained API (well suited for remote invocation)

●Web framework independent layer of domain logic
●Learn once, write everywhere
●Well structured
●Works with large and small projects
●Hard to find existing framework which does the the same work.
Why own architecture?

Where do we use this architecture?
●Perl projects (Mojolicious web framework)
●PHP projects (Slim web framework)
●NodeJs (Express web framework)

Core patterns in the architecture
Application level (Martin Fowler):
●MVC
●Service layer
●Domain model

Class level (GoF):
●Template method
●Command

How do we cook the service layer?

Rule 1: Separate service class for each endpoint

Real code (with meta programming)

NodeJs example of a Service class

Base class (the simplest version)

“run” method
Template method in base class
Guarantees that all procedures are kept:
●Data was validated
●“execute” will be called only after validation
●“execute” will receive only clean data
●Checks permissions before calling “execute”
●Throws exception in case of validation errors.
Can do extra work like caching validator objects, etc.

Perl example of a Service class

Rule 2: There is no excuse for not using Promises or
await/async today


●Error handling - one of the most powerful promise features. You can structure
your error processing in a much better way with promises.
●You will never see “uncaughtException”
●You will have manageable code

Rule 3: Unified approach to validation
●DO NOT TRUST ANY USER INPUT! NEVER!!!
●Declarative validation
●Exclude all fields that do not have validation rules described
●Returns understandable error codes (neither error messages nor numeric
codes)
●It should be clear for the service user what is wrong with his data

We use LIVR for the validation
It supports:
●JavaScript
●Perl
●PHP
●Erlang
●Python
●Ruby
Details are here - http://livr-spec.org/

Rule 4: Never return objects directly
Whitelist every object
property:

1.You know what you
return (that no
internal/secret data there)
2.Your API is stable

It should be clear where any code should be! Otherwise you do not architecture.
One of the risks, than you can end up with an “Anemic domain model”
(https://www.martinfowler.com/bliki/AnemicDomainModel.html)

If you have a large project, this can be a reason of project failure as you will
implicitly switch to “transaction script” approach which is not well suited for large
applications.

Rule 5: Be aware of “Anemic domain model”
antipattern

Full example: User registration

Controllers
Services
Domain model
Data access
layer
Dispatcher
Layers

User registration: router/dispatcher

Routes (Controllers)

Service

User model

Action model

Where can I play with a real code?

We will open source our NodeJs boilerplate and release
several modules to npm soon.
Follow me on Twitter to be notified about it:
http://twitter.com/koorchik
https://twitter.com/webbylab

Viktor Turskyi (koorchik)

[email protected]
https://twitter.com/koorchik https://twitter.com/webbylab
https://github.com/koorchik https://github.com/webbylab






http://webbylab.com