79105790

Date: 2024-10-19 19:53:17
Score: 0.5
Natty:
Report link

I think you are looking for this

or,

https://stackoverflow.com/a/72831318/13086781

This is just a sample code that I wrote like 2 years ago. It should still work.

const express = require("express");
const next = require("next");
const cluster = require("cluster");
const os = require("os");

const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

//if the cluster is master
if (cluster.isMaster) {
  for (let i = 0; i < numCpu; i++) {
    cluster.fork();
  }

  //if worker dies or is killed
  cluster.on("exit", (worker, code, signal) => {
    cluster.fork();
  });
} else {
  app
    .prepare()
    .then(() => {
      const server = express();

      server.all("*", (req, res) => {
        return handle(req, res);
      });

      server.listen(port, (err) => {
        if (err) throw err;
        console.log(`> Ready on http://localhost:${port}`, process.pid);
      });

    })
    .catch((ex) => {
      console.error(ex.stack);
      process.exit(1);
    });
  }
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Gp Adhikari