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:
- Initialization — loading modules, setting up the environment, connecting dependencies.
- Start — executing the main script, setting up servers, event handlers.
- Event Handling — Node.js operates based on the event loop, processing asynchronous operations.
- 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);
});
});
``