windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

Clustering a MERN app in a 4 Cores machine

Asked By: Anonymous

I am trying to cluster a MERN app in a 4 cores server and I am stuck after I have declared the cluster and numCPUs. How should I go about it clustering a MERN app? Many thanks in advance and greatly appreciate any helps. Thanks again.

const express = require("express");
const app = express();
const morgan = require('morgan');
const bodyParser = require("body-parser");
const cors =  require("cors");
const dotenv = require('dotenv');
const connectDB = require("./dbconfig/dbconfig");
const cookieParser = require('cookie-parser');
const path = require('path');
const productRoute = require('./routes/products')
dotenv.config()
connectDB()
const cluster = require('cluster')
const numCPUs = require('os').cpus().length

    const port = process.env.PORT || 5000    

    var whitelist = ['http://localhost:3000', 'http://localhost:5000'];
    app.use(cors({credentials: true, origin: whitelist }));    

    app.use(cookieParser())
    app.use(morgan('dev'))    

    app.use(bodyParser.json({limit: '50mb'}))
    app.use(bodyParser.urlencoded({extended:false}))

    app.use('/api', productRoute)    

    app.use(express.static(path.join("frontend" ,"build")))    

    app.get("*",(req,res) => {
            res.sendFile(path.resolve(__dirname, "frontend","build","index.html"))
        })

    app.listen(port,() => {
        console.log(`Listening to part ${port}...`)
})

Solution

Answered By: Anonymous

You can do something like this.

const express = require("express");
const app = express();
const morgan = require('morgan');
const bodyParser = require("body-parser");
const cors =  require("cors");
const dotenv = require('dotenv');
const connectDB = require("./dbconfig/dbconfig");
const cookieParser = require('cookie-parser');
const path = require('path');
const productRoute = require('./routes/products')
dotenv.config()
connectDB()
const cluster = require('cluster')
const numCPUs = require('os').cpus().length;
const port = process.env.PORT || 5000   

var whitelist = ['http://localhost:3000', 'http://localhost:5000'];
    app.use(cors({credentials: true, origin: whitelist }));    

    app.use(cookieParser())
    app.use(morgan('dev'))    

    app.use(bodyParser.json({limit: '50mb'}))
    app.use(bodyParser.urlencoded({extended:false}))

    app.use('/api', productRoute)    

    app.use(express.static(path.join("frontend" ,"build")))    

    app.get("*",(req,res) => {
            res.sendFile(path.resolve(__dirname, "frontend","build","index.html"))
        })

if (cluster.isMaster) {
    for (var i = 0; i < numCPUs; i++) {
        // Create a worker
        cluster.fork();
    }
} else {
    // Workers share the TCP connection in this server
    // All workers use this port
    app.listen(8080);
}

You can also add the error handling if the worker is closed. For more details, you can check here.

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno