Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
810 B
1
Indexable
Never
const cluster = require('cluster');
const app = require('./config/server');

const startServer = async () => {
    try {
        const PORT = process.env.PORT || 5000;
        await app.listen(PORT);
        console.log(`Server is running on port ${PORT}`);
    } catch (error) {
        console.error('Error starting the server:', error.message);
        process.exit(1);
    }
};

if (cluster.isMaster) {
    // Fork workers.
    const numCPUs = require('os').cpus().length;
    for (let i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on('exit', (worker, code, signal) => {
        console.log(`Worker ${worker.process.pid} died`);
    });
} else {
    // Workers can share any TCP connection
    // In this case, it is an HTTP server
    startServer();
}
Leave a Comment