const {cpus}=require("os")
const cluster=require("cluster")
const cpusLength=cpus().length;
if(cluster.isMaster){
let worker;
for(let i=0;i<cpusLength;i++){
worker= cluster.fork()
worker.on('exit',()=>{
cluster.fork()
})
worker.on("listening",address=>{
console.log(`${address.port}端口启动成功,进程id:${worker.process.pid}`)
})
}
}else{
require("./server/app")
}
子进程
const koa=require("koa")
const app=new koa()
const path=require("path")
const router=require("./router/index")
const bodyParser=require("koa-bodyparser")
const koaViews=require("koa-views")
const views=path.join(__dirname,"./views")
app.use(bodyParser())
app.use(require("koa-static")(__dirname+"./public"))
app.use(koaViews(views,{
extension:'html'
}))
app.use(router.routes(),router.allowedMethods())
const PORT=process.env.PORT||8080
app.listen(PORT)
module.exports=app