SlideShare a Scribd company logo
1 of 40
Download to read offline
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)
viktor@webbylab.com
https://twitter.com/koorchik https://twitter.com/webbylab
https://github.com/koorchik https://github.com/webbylab
http://webbylab.com

More Related Content

What's hot

Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...Matthias Noback
 
Advanced web application architecture - PHP Barcelona
Advanced web application architecture  - PHP BarcelonaAdvanced web application architecture  - PHP Barcelona
Advanced web application architecture - PHP BarcelonaMatthias Noback
 
Алексей Злобин «Scala in Goozy»
Алексей Злобин «Scala in Goozy»Алексей Злобин «Scala in Goozy»
Алексей Злобин «Scala in Goozy»e-Legion
 
Developing Rich Internet Applications with Perl and JavaScript
Developing Rich Internet Applications with Perl and JavaScriptDeveloping Rich Internet Applications with Perl and JavaScript
Developing Rich Internet Applications with Perl and JavaScriptnohuhu
 
ASP.NET MVC Best Practices malisa ncube
ASP.NET MVC Best Practices   malisa ncubeASP.NET MVC Best Practices   malisa ncube
ASP.NET MVC Best Practices malisa ncubeMalisa Ncube
 
Hexagonal Symfony - SymfonyCon Amsterdam 2019
Hexagonal Symfony - SymfonyCon Amsterdam 2019Hexagonal Symfony - SymfonyCon Amsterdam 2019
Hexagonal Symfony - SymfonyCon Amsterdam 2019Matthias Noback
 
Service abstractions - Part 1: Queries
Service abstractions - Part 1: QueriesService abstractions - Part 1: Queries
Service abstractions - Part 1: QueriesMatthias Noback
 
JS digest. January 2018
JS digest. January 2018 JS digest. January 2018
JS digest. January 2018 ElifTech
 
JavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsJavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsPiyush Katariya
 
Robot Framework for beginners and what is new at 2019
Robot Framework for beginners and what is new at 2019Robot Framework for beginners and what is new at 2019
Robot Framework for beginners and what is new at 2019Laura Ojala
 
Focus on the outside, testing in ASP.NET MVC
Focus on the outside, testing in ASP.NET MVCFocus on the outside, testing in ASP.NET MVC
Focus on the outside, testing in ASP.NET MVCRob Ashton
 
Use notes objects in memory and other useful java tips for xpages development
Use notes objects in memory and other useful java tips for xpages developmentUse notes objects in memory and other useful java tips for xpages development
Use notes objects in memory and other useful java tips for xpages developmentFrank van der Linden
 
Java concurrency
Java concurrencyJava concurrency
Java concurrencyfbenault
 
Proxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::StaticperlProxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::Staticperlnohuhu
 
Understanding concurrency
Understanding concurrencyUnderstanding concurrency
Understanding concurrencyAnshul Sharma
 
Api testing libraries using java script an overview
Api testing libraries using java script   an overviewApi testing libraries using java script   an overview
Api testing libraries using java script an overviewvodQA
 
AD111 -- Harnessing the Power of Server-Side JavaScript and Other Advanced XP...
AD111 -- Harnessing the Power of Server-Side JavaScript and Other Advanced XP...AD111 -- Harnessing the Power of Server-Side JavaScript and Other Advanced XP...
AD111 -- Harnessing the Power of Server-Side JavaScript and Other Advanced XP...ddrschiw
 

What's hot (20)

Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...Brutal refactoring, lying code, the Churn, and other emotional stories from L...
Brutal refactoring, lying code, the Churn, and other emotional stories from L...
 
Advanced web application architecture - PHP Barcelona
Advanced web application architecture  - PHP BarcelonaAdvanced web application architecture  - PHP Barcelona
Advanced web application architecture - PHP Barcelona
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Алексей Злобин «Scala in Goozy»
Алексей Злобин «Scala in Goozy»Алексей Злобин «Scala in Goozy»
Алексей Злобин «Scala in Goozy»
 
Hidden Dragons of CGO
Hidden Dragons of CGOHidden Dragons of CGO
Hidden Dragons of CGO
 
Developing Rich Internet Applications with Perl and JavaScript
Developing Rich Internet Applications with Perl and JavaScriptDeveloping Rich Internet Applications with Perl and JavaScript
Developing Rich Internet Applications with Perl and JavaScript
 
ASP.NET MVC Best Practices malisa ncube
ASP.NET MVC Best Practices   malisa ncubeASP.NET MVC Best Practices   malisa ncube
ASP.NET MVC Best Practices malisa ncube
 
Hexagonal Symfony - SymfonyCon Amsterdam 2019
Hexagonal Symfony - SymfonyCon Amsterdam 2019Hexagonal Symfony - SymfonyCon Amsterdam 2019
Hexagonal Symfony - SymfonyCon Amsterdam 2019
 
Service abstractions - Part 1: Queries
Service abstractions - Part 1: QueriesService abstractions - Part 1: Queries
Service abstractions - Part 1: Queries
 
Introduction to Robot Framework
Introduction to Robot FrameworkIntroduction to Robot Framework
Introduction to Robot Framework
 
JS digest. January 2018
JS digest. January 2018 JS digest. January 2018
JS digest. January 2018
 
JavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsJavaScript for Enterprise Applications
JavaScript for Enterprise Applications
 
Robot Framework for beginners and what is new at 2019
Robot Framework for beginners and what is new at 2019Robot Framework for beginners and what is new at 2019
Robot Framework for beginners and what is new at 2019
 
Focus on the outside, testing in ASP.NET MVC
Focus on the outside, testing in ASP.NET MVCFocus on the outside, testing in ASP.NET MVC
Focus on the outside, testing in ASP.NET MVC
 
Use notes objects in memory and other useful java tips for xpages development
Use notes objects in memory and other useful java tips for xpages developmentUse notes objects in memory and other useful java tips for xpages development
Use notes objects in memory and other useful java tips for xpages development
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Proxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::StaticperlProxying DBI with DBD::Gofer and App::Staticperl
Proxying DBI with DBD::Gofer and App::Staticperl
 
Understanding concurrency
Understanding concurrencyUnderstanding concurrency
Understanding concurrency
 
Api testing libraries using java script an overview
Api testing libraries using java script   an overviewApi testing libraries using java script   an overview
Api testing libraries using java script an overview
 
AD111 -- Harnessing the Power of Server-Side JavaScript and Other Advanced XP...
AD111 -- Harnessing the Power of Server-Side JavaScript and Other Advanced XP...AD111 -- Harnessing the Power of Server-Side JavaScript and Other Advanced XP...
AD111 -- Harnessing the Power of Server-Side JavaScript and Other Advanced XP...
 

Similar to The working architecture of NodeJs applications

Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"Fwdays
 
The working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийThe working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийSigma Software
 
Joomla Code Quality Control and Automation Testing
Joomla Code Quality Control and Automation TestingJoomla Code Quality Control and Automation Testing
Joomla Code Quality Control and Automation TestingShyam Sunder Verma
 
Finding and exploiting novel flaws in Java software (SyScan 2015)
Finding and exploiting novel flaws in Java software (SyScan 2015)Finding and exploiting novel flaws in Java software (SyScan 2015)
Finding and exploiting novel flaws in Java software (SyScan 2015)David Jorm
 
Angular2 - A story from the trenches
Angular2 - A story from the trenchesAngular2 - A story from the trenches
Angular2 - A story from the trenchesJohannes Rudolph
 
Rapid app building with loopback framework
Rapid app building with loopback frameworkRapid app building with loopback framework
Rapid app building with loopback frameworkThomas Papaspiros
 
Hunting Security Bugs in Modern Web Applications
Hunting Security Bugs in Modern Web ApplicationsHunting Security Bugs in Modern Web Applications
Hunting Security Bugs in Modern Web ApplicationsToe Khaing
 
The State of the Veil Framework
The State of the Veil FrameworkThe State of the Veil Framework
The State of the Veil FrameworkVeilFramework
 
The Importance Things of Full Stack Development
The Importance Things of Full Stack DevelopmentThe Importance Things of Full Stack Development
The Importance Things of Full Stack DevelopmentMike Taylor
 
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...Binary Studio
 
Hacking Vulnerable Websites to Bypass Firewalls
Hacking Vulnerable Websites to Bypass FirewallsHacking Vulnerable Websites to Bypass Firewalls
Hacking Vulnerable Websites to Bypass FirewallsNetsparker
 
Cloud compiler - Minor Project by students of CBPGEC
Cloud compiler - Minor Project by students of CBPGEC  Cloud compiler - Minor Project by students of CBPGEC
Cloud compiler - Minor Project by students of CBPGEC vipin kumar
 
Serverless Functions and Machine Learning: Putting the AI in APIs
Serverless Functions and Machine Learning: Putting the AI in APIsServerless Functions and Machine Learning: Putting the AI in APIs
Serverless Functions and Machine Learning: Putting the AI in APIsNordic APIs
 
An Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesAn Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesUlrich Krause
 
Intro to .NET for Government Developers
Intro to .NET for Government DevelopersIntro to .NET for Government Developers
Intro to .NET for Government DevelopersFrank La Vigne
 

Similar to The working architecture of NodeJs applications (20)

Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"
 
The working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийThe working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор Турский
 
Joomla Code Quality Control and Automation Testing
Joomla Code Quality Control and Automation TestingJoomla Code Quality Control and Automation Testing
Joomla Code Quality Control and Automation Testing
 
Finding and exploiting novel flaws in Java software (SyScan 2015)
Finding and exploiting novel flaws in Java software (SyScan 2015)Finding and exploiting novel flaws in Java software (SyScan 2015)
Finding and exploiting novel flaws in Java software (SyScan 2015)
 
Node.js Course 2 of 2 - Advanced techniques
Node.js Course 2 of 2 - Advanced techniquesNode.js Course 2 of 2 - Advanced techniques
Node.js Course 2 of 2 - Advanced techniques
 
Angular2 - A story from the trenches
Angular2 - A story from the trenchesAngular2 - A story from the trenches
Angular2 - A story from the trenches
 
Spring Test Framework
Spring Test FrameworkSpring Test Framework
Spring Test Framework
 
Rapid app building with loopback framework
Rapid app building with loopback frameworkRapid app building with loopback framework
Rapid app building with loopback framework
 
.net Framework
.net Framework.net Framework
.net Framework
 
Enterprise Microservices
Enterprise MicroservicesEnterprise Microservices
Enterprise Microservices
 
Hunting Security Bugs in Modern Web Applications
Hunting Security Bugs in Modern Web ApplicationsHunting Security Bugs in Modern Web Applications
Hunting Security Bugs in Modern Web Applications
 
The State of the Veil Framework
The State of the Veil FrameworkThe State of the Veil Framework
The State of the Veil Framework
 
The Importance Things of Full Stack Development
The Importance Things of Full Stack DevelopmentThe Importance Things of Full Stack Development
The Importance Things of Full Stack Development
 
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
Searching for the framework of my dreams in node.js ecosystem by Mykyta Semen...
 
Hacking Vulnerable Websites to Bypass Firewalls
Hacking Vulnerable Websites to Bypass FirewallsHacking Vulnerable Websites to Bypass Firewalls
Hacking Vulnerable Websites to Bypass Firewalls
 
Intro to ember.js
Intro to ember.jsIntro to ember.js
Intro to ember.js
 
Cloud compiler - Minor Project by students of CBPGEC
Cloud compiler - Minor Project by students of CBPGEC  Cloud compiler - Minor Project by students of CBPGEC
Cloud compiler - Minor Project by students of CBPGEC
 
Serverless Functions and Machine Learning: Putting the AI in APIs
Serverless Functions and Machine Learning: Putting the AI in APIsServerless Functions and Machine Learning: Putting the AI in APIs
Serverless Functions and Machine Learning: Putting the AI in APIs
 
An Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesAn Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPages
 
Intro to .NET for Government Developers
Intro to .NET for Government DevelopersIntro to .NET for Government Developers
Intro to .NET for Government Developers
 

More from Viktor Turskyi

How to create a high performance excel engine in java script
How to create a high performance excel engine in java scriptHow to create a high performance excel engine in java script
How to create a high performance excel engine in java scriptViktor Turskyi
 
Livr 2.0 in JS - Vinnytsia.JS 2019
Livr 2.0 in JS - Vinnytsia.JS 2019Livr 2.0 in JS - Vinnytsia.JS 2019
Livr 2.0 in JS - Vinnytsia.JS 2019Viktor Turskyi
 
Yet another json rpc library (mole rpc)
Yet another json rpc library (mole rpc)Yet another json rpc library (mole rpc)
Yet another json rpc library (mole rpc)Viktor Turskyi
 
KharkivJS 2018 Information Security Practice
KharkivJS 2018 Information Security PracticeKharkivJS 2018 Information Security Practice
KharkivJS 2018 Information Security PracticeViktor Turskyi
 
"Offline mode for a mobile application, redux on server and a little bit abou...
"Offline mode for a mobile application, redux on server and a little bit abou..."Offline mode for a mobile application, redux on server and a little bit abou...
"Offline mode for a mobile application, redux on server and a little bit abou...Viktor Turskyi
 
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017Viktor Turskyi
 
How to extract information from text with Semgrex
How to extract information from text with SemgrexHow to extract information from text with Semgrex
How to extract information from text with SemgrexViktor Turskyi
 
How to translate your Single Page Application - Webcamp 2016 (en)
How to translate your Single Page Application - Webcamp 2016 (en)How to translate your Single Page Application - Webcamp 2016 (en)
How to translate your Single Page Application - Webcamp 2016 (en)Viktor Turskyi
 
JS Lab 2016 - Frontend trends 2015 - 2016
JS Lab 2016 - Frontend trends 2015 - 2016JS Lab 2016 - Frontend trends 2015 - 2016
JS Lab 2016 - Frontend trends 2015 - 2016Viktor Turskyi
 
It's Quiz - Cloud testing platform
It's Quiz - Cloud testing platformIt's Quiz - Cloud testing platform
It's Quiz - Cloud testing platformViktor Turskyi
 
Kharkiv JS 2015 - Creating isomorphic applications in React (en)
Kharkiv JS  2015 - Creating isomorphic applications in React (en)Kharkiv JS  2015 - Creating isomorphic applications in React (en)
Kharkiv JS 2015 - Creating isomorphic applications in React (en)Viktor Turskyi
 
Kharkiv JS 2015: Боль и радость создания изоморфных приложений на ReactJS (RU)
Kharkiv JS  2015: Боль и радость создания изоморфных приложений на ReactJS (RU)Kharkiv JS  2015: Боль и радость создания изоморфных приложений на ReactJS (RU)
Kharkiv JS 2015: Боль и радость создания изоморфных приложений на ReactJS (RU)Viktor Turskyi
 
Language Independent Validation Rules (LIVR)
Language Independent Validation Rules (LIVR)Language Independent Validation Rules (LIVR)
Language Independent Validation Rules (LIVR)Viktor Turskyi
 
Mapreduce in JavaScript
Mapreduce in JavaScriptMapreduce in JavaScript
Mapreduce in JavaScriptViktor Turskyi
 
Testing orm based code
Testing orm based codeTesting orm based code
Testing orm based codeViktor Turskyi
 
Maybe you do not know that ...
Maybe you do not know that ...Maybe you do not know that ...
Maybe you do not know that ...Viktor Turskyi
 

More from Viktor Turskyi (18)

How to create a high performance excel engine in java script
How to create a high performance excel engine in java scriptHow to create a high performance excel engine in java script
How to create a high performance excel engine in java script
 
Livr 2.0 in JS - Vinnytsia.JS 2019
Livr 2.0 in JS - Vinnytsia.JS 2019Livr 2.0 in JS - Vinnytsia.JS 2019
Livr 2.0 in JS - Vinnytsia.JS 2019
 
Yet another json rpc library (mole rpc)
Yet another json rpc library (mole rpc)Yet another json rpc library (mole rpc)
Yet another json rpc library (mole rpc)
 
KharkivJS 2018 Information Security Practice
KharkivJS 2018 Information Security PracticeKharkivJS 2018 Information Security Practice
KharkivJS 2018 Information Security Practice
 
"Offline mode for a mobile application, redux on server and a little bit abou...
"Offline mode for a mobile application, redux on server and a little bit abou..."Offline mode for a mobile application, redux on server and a little bit abou...
"Offline mode for a mobile application, redux on server and a little bit abou...
 
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017
Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017
 
How to extract information from text with Semgrex
How to extract information from text with SemgrexHow to extract information from text with Semgrex
How to extract information from text with Semgrex
 
How to translate your Single Page Application - Webcamp 2016 (en)
How to translate your Single Page Application - Webcamp 2016 (en)How to translate your Single Page Application - Webcamp 2016 (en)
How to translate your Single Page Application - Webcamp 2016 (en)
 
JS Lab 2016 - Frontend trends 2015 - 2016
JS Lab 2016 - Frontend trends 2015 - 2016JS Lab 2016 - Frontend trends 2015 - 2016
JS Lab 2016 - Frontend trends 2015 - 2016
 
It's Quiz - Cloud testing platform
It's Quiz - Cloud testing platformIt's Quiz - Cloud testing platform
It's Quiz - Cloud testing platform
 
Kharkiv JS 2015 - Creating isomorphic applications in React (en)
Kharkiv JS  2015 - Creating isomorphic applications in React (en)Kharkiv JS  2015 - Creating isomorphic applications in React (en)
Kharkiv JS 2015 - Creating isomorphic applications in React (en)
 
Kharkiv JS 2015: Боль и радость создания изоморфных приложений на ReactJS (RU)
Kharkiv JS  2015: Боль и радость создания изоморфных приложений на ReactJS (RU)Kharkiv JS  2015: Боль и радость создания изоморфных приложений на ReactJS (RU)
Kharkiv JS 2015: Боль и радость создания изоморфных приложений на ReactJS (RU)
 
Language Independent Validation Rules (LIVR)
Language Independent Validation Rules (LIVR)Language Independent Validation Rules (LIVR)
Language Independent Validation Rules (LIVR)
 
Hadoop webcamp 2015
Hadoop webcamp 2015 Hadoop webcamp 2015
Hadoop webcamp 2015
 
Mapreduce in JavaScript
Mapreduce in JavaScriptMapreduce in JavaScript
Mapreduce in JavaScript
 
Excel in Javascript
Excel in JavascriptExcel in Javascript
Excel in Javascript
 
Testing orm based code
Testing orm based codeTesting orm based code
Testing orm based code
 
Maybe you do not know that ...
Maybe you do not know that ...Maybe you do not know that ...
Maybe you do not know that ...
 

Recently uploaded

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

The working architecture of NodeJs applications

  • 1. The working architecture of NodeJs applications Viktor Turskyi CTO at WebbyLab Kyiv, Ukraine
  • 2. About me ● CTO at WebbyLab ● Open Source developer (Perl, JavaScript) ● 13 years in Software development ● Successfully delivered more than 60 projects
  • 3. Why to talk about the architecture? 99% of examples in Internet - “hello world” with express/sails etc. A lot of misunderstandings around architectural patterns
  • 5. Where to place this code? Model or Controller?
  • 6. 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/
  • 7. Is Model (MVC) and Domain Model the same?
  • 8. Model (from MVC)/Domain Logic ● Domain model ● Transaction script ● Table module ● Service layer
  • 10. 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
  • 11. 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)
  • 12. 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)
  • 13. ● 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?
  • 14. Where do we use this architecture? ● Perl projects (Mojolicious web framework) ● PHP projects (Slim web framework) ● NodeJs (Express web framework)
  • 15. Core patterns in the architecture Application level (Martin Fowler): ● MVC ● Service layer ● Domain model Class level (GoF): ● Template method ● Command
  • 16. How do we cook the service layer?
  • 17. Rule 1: Separate service class for each endpoint
  • 18. Real code (with meta programming)
  • 19. NodeJs example of a Service class
  • 20.
  • 21. Base class (the simplest version)
  • 22. “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.
  • 23. Perl example of a Service class
  • 24.
  • 25.
  • 26. 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
  • 27. 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
  • 28. We use LIVR for the validation It supports: ● JavaScript ● Perl ● PHP ● Erlang ● Python ● Ruby Details are here - http://livr-spec.org/
  • 29. 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
  • 30. 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
  • 31. Full example: User registration
  • 36.
  • 39. 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
  • 40. Viktor Turskyi (koorchik) viktor@webbylab.com https://twitter.com/koorchik https://twitter.com/webbylab https://github.com/koorchik https://github.com/webbylab http://webbylab.com