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
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