nodejs 다 중 핵심 예제

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
//var numCPUs = 4;

if (cluster.isMaster) {
    console.log("master start...");

    // Fork workers.
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on('listening',function(worker,address){
        console.log('listening: worker ' + worker.process.pid +', Address: '+address.address+":"+address.port);
    });

    cluster.on('exit', function(worker, code, signal) {
        console.log('worker ' + worker.process.pid + ' died');
    });
} else {
    http.createServer(function(req, res) {
        res.writeHead(200, {'Content-Type': 'text/html;charset=UTF-8'});
        res.write('<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>Node  </title></head><body>'+new Date().getTime()+'</body></html>');
        res.end();
    }).listen(4040);
}

좋은 웹페이지 즐겨찾기