Skip to content

Instantly share code, notes, and snippets.

@AliMD

AliMD/server.js

Last active Nov 29, 2016
Embed
What would you like to do?
Nodejs Multi Cluster HTTP Server Example
"use strict";
let http = require('http');
let host = '127.0.0.1', port = 8080;
let cluster = require('cluster');
let numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Hey, I'm Master!`);
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
http
.createServer(serverListener)
.listen(port, host)
;
console.log(`New server on http://${host}:${port}/`);
}
function serverListener (request, response) {
response.end('Hi ;)');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment