forked from amir20/phantomjs-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster.js
More file actions
38 lines (32 loc) · 1.02 KB
/
cluster.js
File metadata and controls
38 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var cluster = require("cluster");
if (cluster.isMaster) {
var cpuCount = require('os').cpus().length;
for (var i = 0; i < cpuCount; i++) {
console.log('Forking process #' + (i + 1));
cluster.fork();
}
cluster.on('exit', function (worker) {
console.log('Worker ' + worker.id + ' died. Forking...');
cluster.fork();
});
} else {
var phantom = require("phantom"),
express = require("express"),
serve = express();
serve.get('/foo', function (req, res) {
phantom.create().then(function (ph) {
ph.createPage().then(function (page) {
page.setting('userAgent', 'foo app');
page.open('http://localhost:8080/test.html').then(function (status) {
res.json({
pageStatus: status
});
page.close();
ph.exit();
});
});
});
}).listen(3000);
}
// npm install express
// node cluster.js