Skip to content

Instantly share code, notes, and snippets.

@jalasem
Created November 13, 2018 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalasem/db78859b60287352b56feb1e14d9221b to your computer and use it in GitHub Desktop.
Save jalasem/db78859b60287352b56feb1e14d9221b to your computer and use it in GitHub Desktop.
const cluster = require('cluster')
const express = require('express')
const fs = require('fs')
const CPUs = require('os')
.cpus()
.length
if (cluster.isMaster) {
console.log(`Master cluster setting up ${CPUs} workers...`)
for (var i = 0; i < CPUs; i++) {
cluster.fork()
}
cluster.on('online', worker => {
console.log(`Worker ${worker.process.pid} is online.`)
})
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died with code: ${code}, and signal: ${signal}`)
console.log('Starting a new worker')
cluster.fork()
})
} else {
const http = require('http')
// const path = require('path')
const app = express()
app
.use(helmet())
.disable('x-powered-by')
.use(cors())
app.get('/', (req, res) => {
console.log(`process pid:${process.pid} recvd a request. at '/'`)
res.send(`<h1>Hey!, I'm healthy!</h1>`)
})
// const secureServerOptions = {
// key: fs.readFileSync(path.resolve('./src/cert/private.key')),
// cert: fs.readFileSync(path.resolve('./src/cert/certificate.crt')),
// ca: fs.readFileSync(path.resolve('./src/cert/ca_bundle.pem')),
// requestCert: true,
// rejectUnauthorized: false
// }
// const secureServer = https.createServer(secureServerOptions, app)
const server = http.createServer(app)
// secureServer.listen(S_PORT)
server.listen(PORT, () => {
console.log(`Server(${process.pid}) running on PORT:${PORT} is listening to all requests`)
})
server.listen(PORT + 1, () => {
console.log(`Server(${process.pid}) running on PORT:${PORT + 1} is listening to all requests`)
})
server.listen(PORT + 2, () => {
console.log(`Server(${process.pid}) running on PORT:${PORT + 2} is listening to all requests`)
})
server.listen(PORT + 3, () => {
console.log(`Server(${process.pid}) running on PORT:${PORT + 3} is listening to all requests`)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment