Compare commits
1 Commits
multi-proc
...
update-to-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f33fd473aa |
@@ -10,7 +10,7 @@ To deploy a frontend-only React app, use the static-site optimized
|
|||||||
|
|
||||||
## Design Points
|
## Design Points
|
||||||
|
|
||||||
A combo of two npm projects, the backend server and the frontend UI. So there are two `package.json` configs and thereforce two places to run `npm` commands:
|
A combo of two npm projects, the backend server and the frontend UI. So there are two `package.json` configs.
|
||||||
|
|
||||||
1. [`package.json`](package.json) for [Node server](server/) & [Heroku deploy](https://devcenter.heroku.com/categories/deployment)
|
1. [`package.json`](package.json) for [Node server](server/) & [Heroku deploy](https://devcenter.heroku.com/categories/deployment)
|
||||||
* `heroku-postbuild` script compiles the webpack bundle during deploy
|
* `heroku-postbuild` script compiles the webpack bundle during deploy
|
||||||
@@ -18,7 +18,6 @@ A combo of two npm projects, the backend server and the frontend UI. So there ar
|
|||||||
2. [`react-ui/package.json`](react-ui/package.json) for [React web UI](react-ui/)
|
2. [`react-ui/package.json`](react-ui/package.json) for [React web UI](react-ui/)
|
||||||
* generated by [create-react-app](https://github.com/facebookincubator/create-react-app)
|
* generated by [create-react-app](https://github.com/facebookincubator/create-react-app)
|
||||||
|
|
||||||
Includes a minimal [Node Cluster](https://nodejs.org/docs/latest-v8.x/api/cluster.html) [implementation](server/index.js) to parallelize the single-threaded Node process across the available CPU cores.
|
|
||||||
|
|
||||||
## Demo
|
## Demo
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +1,8 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const cluster = require('cluster');
|
|
||||||
const numCPUs = require('os').cpus().length;
|
|
||||||
|
|
||||||
const PORT = process.env.PORT || 5000;
|
|
||||||
|
|
||||||
// Multi-process to utilize all CPU cores.
|
|
||||||
if (cluster.isMaster) {
|
|
||||||
console.error(`Node cluster master ${process.pid} is running`);
|
|
||||||
|
|
||||||
// Fork workers.
|
|
||||||
for (let i = 0; i < numCPUs; i++) {
|
|
||||||
cluster.fork();
|
|
||||||
}
|
|
||||||
|
|
||||||
cluster.on('exit', (worker, code, signal) => {
|
|
||||||
console.error(`Node cluster worker ${worker.process.pid} exited: code ${code}, signal ${signal}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
} else {
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
const PORT = process.env.PORT || 5000;
|
||||||
|
|
||||||
// Priority serve any static files.
|
// Priority serve any static files.
|
||||||
app.use(express.static(path.resolve(__dirname, '../react-ui/build')));
|
app.use(express.static(path.resolve(__dirname, '../react-ui/build')));
|
||||||
@@ -36,6 +19,5 @@ if (cluster.isMaster) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.listen(PORT, function () {
|
app.listen(PORT, function () {
|
||||||
console.error(`Node cluster worker ${process.pid}: listening on port ${PORT}`);
|
console.log(`Listening on port ${PORT}`);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user