Computer scienceBackendNode.jsCore ConceptsInternal modulesOther modules

Introduction to net module

Error Handling in TCP Server

Report a typo

You're developing a TCP server in Node.js and want to implement error handling for the EADDRINUSE error. You need to listen for the 'error' event on the server object, check if the error code is EADDRINUSE, and then retry listening on the same port after a delay of 1 second.

Rearrange the lines of code below to form a correct program that handles this specific error.

Reorder lines using drag or arrows. Adjust indentation with left buttons
                server.close();
              
                console.log('Address in use, retrying...');
              
                server.on('error', (error) => {
              
                setTimeout(() => {
              
                });
              
                }, 1000);
              
                console.error('An error occurred while starting the server:', error);
              
                console.log(`Server is listening on port ${port}`);
              
                }
              
                } else {
              
                server.listen(port, () => {
              
                if (error.code === 'EADDRINUSE') {
              
                });
              
___

Create a free account to access the full topic