Sobes.tech
Junior — Middle

Can you name the main stages of the Node.js application lifecycle?

sobes.tech AI

Answer from AI

The lifecycle of a Node.js application can be represented by the following main stages:

  1. Initialization — loading modules, setting up the environment, connecting dependencies.
  2. Start — executing the main script, setting up servers, event handlers.
  3. Event Handling — Node.js operates based on the event loop, processing asynchronous operations.
  4. Shutdown — properly closing servers, releasing resources, handling termination signals (e.g., SIGINT).

Example of a simple lifecycle:

const http = require('http');

const server = http.createServer((req, res) => {
  res.end('Hello World');
});

server.listen(3000, () => {
  console.log('Server started on port 3000');
});

process.on('SIGINT', () => {
  server.close(() => {
    console.log('Server closed');
    process.exit(0);
  });
});
``