SlideShare a Scribd company logo
1 of 23
Download to read offline
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Visualize Future
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Common Mistake In Nodejs
nguyentvk@uiza.io
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Introduction
Forget close connection
Blocking the event loop
Executing a callback multiple times
Callback Hell
Best practice
Reference
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Introduction
- Node.js has seen an important growth in the past years, with big companies such
as Netflix, Linkedin, PayPal adopting it
- More and more people are picking up Node and publishing modules to NPM at
such a pace that exceeds other languages
- In this article we will talk about the most common mistakes Node developers
make and how to avoid them
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Forget close connection
RabbitMQ
- In the past, Uiza had a problem with rabbitMQ about connection/channel. This problem happen
when migration data for TOPICA. RabbitMQ was not accept any connection, so message couldn’t
delivery to special queue. Detail error was “ECONNREFUSED”.
- After two days later, reason was found “not close channel after create”. The limit connection of
rabbitMQ is about 1024 by default . When you forgot close channel, number connection grow up
very fast. When RabbitMQ reaches this limit, it can't accept more connections.
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Forget close connection
RabbitMQ
- Solution
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Forget close connection
Mysql pool connection
- In the past, Uiza also had a problem with pool connection. In this case, many service couldn’t connect
to mysql server, then all most apis response with error code 504. Detail error was “ECONNRESET”.
- This is a critical error. After few hours later, we found reason was found reason and solution. A few
connection didn’t release after query done. So when number connection is equal limit connection of
pool.
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Forget close connection
Mysql pool connection
- Solution
Read more: https://www.npmjs.com/package/mysql#connection-options
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Blocking the event loop
- Node.js runs on a single thread, everything that will block the event loop will block
everything
- That means that if you have a web server with a thousand connected clients and you happen
to block the event loop, block everything.
=> So handling input-output operations asynchronously
Example:
- read sync content from big file
- Parse json from big data
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Executing a callback multiple times
Have you ever seen this error ? “Error: Can't set headers after they are sent.”
If yes, you called a callback multiple times
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Executing a callback multiple times
How many callback was called ?
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Callback Hell
Callbacks are just the name of a convention for using JavaScript functions
Callback hell is caused by poor coding practices
How do you feel when reading code
like this?
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Callback Hell
using async lib: https://github.com/caolan/async/blob/v1.5.2/README.md
Control Flow: waterfall, parallel, series, times
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Callback Hell
using Promise: async await
- The resulting code is much cleaner.
- Error handling is much simpler and it relies on try/catch
- Debugging is much simpler
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Error handling
Class Error
Error objects capture a "stack trace" detailing the point in the code at which the Error was instantiated, and may
provide a text description of the error.
All errors generated by Node.js, including all System and JavaScript errors, will either be instances of, or inherit
from, the Error class.
error.code# The error.code property is a string label that identifies the kind of error. error.code is the most stable way
to identify an error.
error.message# The error.message property is the string description of the error as set by calling new
Error(message).
error.stack# The error.stack property is a string describing the point in the code at which the Error was instantiated.
More detail: https://nodejs.org/api/errors.html#errors_error_propagation_and_interception
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Error handling - Synchronous programming
The try statement lets you test a block of code for errors.
The catch statement lets you handle the error.
The throw statement lets you create custom errors.
The finally statement lets you execute code, after try and catch, regardless of the result.
But with asynchronous throw
error in callback alway right?
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Error handling - Asynchronous programming
In this case,
- CATCH not called
- You just receive a error message and
Server has been stoped
=> Why?
- You are throwing error inside an
asynchronous setTimeout call, which will
lead to an uncaught error.
- The asynchronous code will not execute
in the same context as the try-catch
block.
- This has nothing to do with the promise
API. This is just part of the behavior of
asynchronous code execution in
JavaScript.
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
DEMO
Error handling - Asynchronous programming
Async caolan mixing async/await
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Error handling - Asynchronous programming
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Async caolan mixing async/await
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Q&A
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Reference
Top 10 Mistakes Node.js Developers Makehttps://www.airpair.com/node.js/posts/top-10-
mistakes-node-developers-make
Common Mistakes That Node.js Developers Make
https://medium.com/swlh/common-mistakes-that-node-js-developers-make-9df7106d09f1
2018 UIZA PTE. LTD. ALL RIGHTS RESERVED.
Thank You

More Related Content

Similar to Common mistake in nodejs

Spring Boot & Spring Cloud on PAS- Nate Schutta (2/2)
Spring Boot & Spring Cloud on PAS- Nate Schutta (2/2)Spring Boot & Spring Cloud on PAS- Nate Schutta (2/2)
Spring Boot & Spring Cloud on PAS- Nate Schutta (2/2)VMware Tanzu
 
How to Make Your NodeJS Application Secure (24 Best Security Tips )
How to Make Your NodeJS Application Secure (24 Best Security Tips )How to Make Your NodeJS Application Secure (24 Best Security Tips )
How to Make Your NodeJS Application Secure (24 Best Security Tips )Katy Slemon
 
Production debugging web applications
Production debugging web applicationsProduction debugging web applications
Production debugging web applicationsIdo Flatow
 
44CON 2014 - Switches Get Stitches, Eireann Leverett & Matt Erasmus
44CON 2014 - Switches Get Stitches,  Eireann Leverett & Matt Erasmus44CON 2014 - Switches Get Stitches,  Eireann Leverett & Matt Erasmus
44CON 2014 - Switches Get Stitches, Eireann Leverett & Matt Erasmus44CON
 
Attacking Pipelines--Security meets Continuous Delivery
Attacking Pipelines--Security meets Continuous DeliveryAttacking Pipelines--Security meets Continuous Delivery
Attacking Pipelines--Security meets Continuous DeliveryJames Wickett
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5Peter Lawrey
 
How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.PVS-Studio
 
How to make fewer errors at the stage of code writing. Part N1
How to make fewer errors at the stage of code writing. Part N1How to make fewer errors at the stage of code writing. Part N1
How to make fewer errors at the stage of code writing. Part N1Andrey Karpov
 
2019 StartIT - Boosting your performance with Blackfire
2019 StartIT - Boosting your performance with Blackfire2019 StartIT - Boosting your performance with Blackfire
2019 StartIT - Boosting your performance with BlackfireMarko Mitranić
 
Modern Web Security, Lazy but Mindful Like a Fox
Modern Web Security, Lazy but Mindful Like a FoxModern Web Security, Lazy but Mindful Like a Fox
Modern Web Security, Lazy but Mindful Like a FoxC4Media
 
Securing Rails
Securing RailsSecuring Rails
Securing RailsAlex Payne
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio
 
Perfomatix - NodeJS Coding Standards
Perfomatix - NodeJS Coding StandardsPerfomatix - NodeJS Coding Standards
Perfomatix - NodeJS Coding StandardsPerfomatix Solutions
 
The Next Five Years of Rails
The Next Five Years of RailsThe Next Five Years of Rails
The Next Five Years of RailsAlex Mercer
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteDr Nic Williams
 
Shift-left SRE: Self-healing on OpenShift with Ansible
Shift-left SRE: Self-healing on OpenShift with AnsibleShift-left SRE: Self-healing on OpenShift with Ansible
Shift-left SRE: Self-healing on OpenShift with AnsibleJürgen Etzlstorfer
 
Multi-tenancy with Rails
Multi-tenancy with RailsMulti-tenancy with Rails
Multi-tenancy with RailsPaul Gallagher
 

Similar to Common mistake in nodejs (20)

Spring Boot & Spring Cloud on PAS- Nate Schutta (2/2)
Spring Boot & Spring Cloud on PAS- Nate Schutta (2/2)Spring Boot & Spring Cloud on PAS- Nate Schutta (2/2)
Spring Boot & Spring Cloud on PAS- Nate Schutta (2/2)
 
How to Make Your NodeJS Application Secure (24 Best Security Tips )
How to Make Your NodeJS Application Secure (24 Best Security Tips )How to Make Your NodeJS Application Secure (24 Best Security Tips )
How to Make Your NodeJS Application Secure (24 Best Security Tips )
 
Production debugging web applications
Production debugging web applicationsProduction debugging web applications
Production debugging web applications
 
44CON 2014 - Switches Get Stitches, Eireann Leverett & Matt Erasmus
44CON 2014 - Switches Get Stitches,  Eireann Leverett & Matt Erasmus44CON 2014 - Switches Get Stitches,  Eireann Leverett & Matt Erasmus
44CON 2014 - Switches Get Stitches, Eireann Leverett & Matt Erasmus
 
Agility Requires Safety
Agility Requires SafetyAgility Requires Safety
Agility Requires Safety
 
Attacking Pipelines--Security meets Continuous Delivery
Attacking Pipelines--Security meets Continuous DeliveryAttacking Pipelines--Security meets Continuous Delivery
Attacking Pipelines--Security meets Continuous Delivery
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5
 
How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.How to make fewer errors at the stage of code writing. Part N1.
How to make fewer errors at the stage of code writing. Part N1.
 
How to make fewer errors at the stage of code writing. Part N1
How to make fewer errors at the stage of code writing. Part N1How to make fewer errors at the stage of code writing. Part N1
How to make fewer errors at the stage of code writing. Part N1
 
2019 StartIT - Boosting your performance with Blackfire
2019 StartIT - Boosting your performance with Blackfire2019 StartIT - Boosting your performance with Blackfire
2019 StartIT - Boosting your performance with Blackfire
 
Modern Web Security, Lazy but Mindful Like a Fox
Modern Web Security, Lazy but Mindful Like a FoxModern Web Security, Lazy but Mindful Like a Fox
Modern Web Security, Lazy but Mindful Like a Fox
 
Securing Rails
Securing RailsSecuring Rails
Securing Rails
 
Asynchronyin net
Asynchronyin netAsynchronyin net
Asynchronyin net
 
PVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's codePVS-Studio: analyzing ReactOS's code
PVS-Studio: analyzing ReactOS's code
 
Perfomatix - NodeJS Coding Standards
Perfomatix - NodeJS Coding StandardsPerfomatix - NodeJS Coding Standards
Perfomatix - NodeJS Coding Standards
 
The Next Five Years of Rails
The Next Five Years of RailsThe Next Five Years of Rails
The Next Five Years of Rails
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
 
Shift-left SRE: Self-healing on OpenShift with Ansible
Shift-left SRE: Self-healing on OpenShift with AnsibleShift-left SRE: Self-healing on OpenShift with Ansible
Shift-left SRE: Self-healing on OpenShift with Ansible
 
Defense at Scale
Defense at ScaleDefense at Scale
Defense at Scale
 
Multi-tenancy with Rails
Multi-tenancy with RailsMulti-tenancy with Rails
Multi-tenancy with Rails
 

Recently uploaded

(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 

Recently uploaded (20)

(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 

Common mistake in nodejs

  • 1. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Visualize Future
  • 2. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Common Mistake In Nodejs nguyentvk@uiza.io
  • 3. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Introduction Forget close connection Blocking the event loop Executing a callback multiple times Callback Hell Best practice Reference
  • 4. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Introduction - Node.js has seen an important growth in the past years, with big companies such as Netflix, Linkedin, PayPal adopting it - More and more people are picking up Node and publishing modules to NPM at such a pace that exceeds other languages - In this article we will talk about the most common mistakes Node developers make and how to avoid them
  • 5. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Forget close connection RabbitMQ - In the past, Uiza had a problem with rabbitMQ about connection/channel. This problem happen when migration data for TOPICA. RabbitMQ was not accept any connection, so message couldn’t delivery to special queue. Detail error was “ECONNREFUSED”. - After two days later, reason was found “not close channel after create”. The limit connection of rabbitMQ is about 1024 by default . When you forgot close channel, number connection grow up very fast. When RabbitMQ reaches this limit, it can't accept more connections.
  • 6. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Forget close connection RabbitMQ - Solution
  • 7. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Forget close connection Mysql pool connection - In the past, Uiza also had a problem with pool connection. In this case, many service couldn’t connect to mysql server, then all most apis response with error code 504. Detail error was “ECONNRESET”. - This is a critical error. After few hours later, we found reason was found reason and solution. A few connection didn’t release after query done. So when number connection is equal limit connection of pool.
  • 8. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Forget close connection Mysql pool connection - Solution Read more: https://www.npmjs.com/package/mysql#connection-options
  • 9. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Blocking the event loop - Node.js runs on a single thread, everything that will block the event loop will block everything - That means that if you have a web server with a thousand connected clients and you happen to block the event loop, block everything. => So handling input-output operations asynchronously Example: - read sync content from big file - Parse json from big data
  • 10. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Executing a callback multiple times Have you ever seen this error ? “Error: Can't set headers after they are sent.” If yes, you called a callback multiple times
  • 11. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Executing a callback multiple times How many callback was called ?
  • 12. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Callback Hell Callbacks are just the name of a convention for using JavaScript functions Callback hell is caused by poor coding practices How do you feel when reading code like this?
  • 13. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Callback Hell using async lib: https://github.com/caolan/async/blob/v1.5.2/README.md Control Flow: waterfall, parallel, series, times
  • 14. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Callback Hell using Promise: async await - The resulting code is much cleaner. - Error handling is much simpler and it relies on try/catch - Debugging is much simpler
  • 15. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Error handling Class Error Error objects capture a "stack trace" detailing the point in the code at which the Error was instantiated, and may provide a text description of the error. All errors generated by Node.js, including all System and JavaScript errors, will either be instances of, or inherit from, the Error class. error.code# The error.code property is a string label that identifies the kind of error. error.code is the most stable way to identify an error. error.message# The error.message property is the string description of the error as set by calling new Error(message). error.stack# The error.stack property is a string describing the point in the code at which the Error was instantiated. More detail: https://nodejs.org/api/errors.html#errors_error_propagation_and_interception
  • 16. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Error handling - Synchronous programming The try statement lets you test a block of code for errors. The catch statement lets you handle the error. The throw statement lets you create custom errors. The finally statement lets you execute code, after try and catch, regardless of the result. But with asynchronous throw error in callback alway right?
  • 17. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Error handling - Asynchronous programming In this case, - CATCH not called - You just receive a error message and Server has been stoped => Why? - You are throwing error inside an asynchronous setTimeout call, which will lead to an uncaught error. - The asynchronous code will not execute in the same context as the try-catch block. - This has nothing to do with the promise API. This is just part of the behavior of asynchronous code execution in JavaScript.
  • 18. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. DEMO Error handling - Asynchronous programming Async caolan mixing async/await
  • 19. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Error handling - Asynchronous programming
  • 20. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Async caolan mixing async/await
  • 21. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Q&A
  • 22. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Reference Top 10 Mistakes Node.js Developers Makehttps://www.airpair.com/node.js/posts/top-10- mistakes-node-developers-make Common Mistakes That Node.js Developers Make https://medium.com/swlh/common-mistakes-that-node-js-developers-make-9df7106d09f1
  • 23. 2018 UIZA PTE. LTD. ALL RIGHTS RESERVED. Thank You