Patrones de dise帽o en Node.js
Qu茅 es Node.js y c贸mo impulsa tu negocio
Patrones de dise帽o esenciales en Node.js
Patr贸n Singleton y Factory en JavaScript
Implementaci贸n pr谩ctica de Singleton y Factory en JavaScript
Implementaci贸n del patr贸n Observer con EventEmitter en Node.js
Implementaci贸n de Middlewares en Node.js sin Express
Decorators e inyecci贸n de dependencias en JavaScript
Flujo de Datos con Node.js
Aprende qu茅 son Buffer y Streams en Node.js
C贸mo utilizar streams y pipelines en Node.js
C贸mo funciona el Event Loop en Node.js
Qu茅 es Libuv y c贸mo maneja la asincron铆a en Node.js
Estrategias para ejecutar c贸digo as铆ncrono en Node.js
Debugging y Diagn贸stico en Node.js
C贸mo utilizar el Debugger en Node.js para solucionar problemas
Uso de Diagnostic Channels en Node.js para observabilidad y diagn贸stico
Instrumentaci贸n y m茅tricas clave en performance para aplicaciones Node.js
Control de errores globales y manejo de se帽ales en Node.js
Implementaci贸n Eficiente de Logs con Pino en Node.js
Performance en Node.js
An谩lisis del event loop en aplicaciones Node.js usando Nsolid
C贸mo Diagnosticar y Solucionar Memory Leaks en Aplicaciones Node.js
Optimizar rendimiento en Node.js con Worker Threads y Child Processes
Optimiza y Escala Aplicaciones Node.js con T茅cnicas de Caching
Creando CLIs con Node.js
C贸mo crear aplicaciones CLI con Node.js
C贸mo Crear un CLI con Minimist y Manejar Argumentos en Node.js
Creaci贸n de un CLI con Node.js y Google Generative AI
Creaci贸n de Chat con IA usando CLI en Node
C贸mo Crear e Instalar tu Propio CLI de Node con npm
You don't have access to this class
Keep learning! Join and start boosting your career
Creating robust applications in Node.js involves properly handling global errors and system signals. Many developers overlook global events that allow to control unexpected situations, so it is important to integrate and understand them in order to maintain the stability of the application.
The event called uncaughtException allows to capture errors that are not explicitly controlled within the code. When an unexpected error is generated and it is not inside a block that can handle it, this event is fired automatically informing about the error and avoiding the uncontrolled crash of the application.
To implement it, it is only necessary to add it to the process module as follows:
process.on('uncaughtException', (error) => { console.error('An uncontrolled error was caught:', error);});
This makes it easy to identify exactly where the problem occurred and correct it quickly.
The unhandledRejection event detects and controls rejections of promises that do not have catch events defined. This type of rejections are frequent in applications that use promises and can cause unexpected failures.
Adding this event is simple:
process.on('unhandledRejection', (reason, promise) => { console.error('Promise rejected and not handled:', reason);});
By having this control, it is possible to clearly diagnose rejections without explicit handling and maintain continuous operation of the server.
System signals tell the application to perform a controlled exit, an orderly shutdown that maintains the integrity of active processes. Some common signals are:
SIGINT
: Issued when control C is pressed.SIGTERM
: Signal to terminate programs sent by external sources.Implementing them is possible by listening to each signal and managing the shutdown:
['SIGINT', 'SIGTERM', 'SIGUSR1', 'SIGUSR2'].forEach((signal) => { process.on(signal, () => { console.log(`Received signal ${signal}`); process.exit(1); // Controlled exit indicating error. });});
This approach allows the application to terminate its processes cleanly and in a safe state when it detects shutdowns from the operating system.
I invite you to share your experience controlling global events and signals in your Node.js projects; tell me how you have solved unexpected errors or abrupt exits so far.
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?