3 Commits

Author SHA1 Message Date
Mars Hall
838b163487 📚 multi-processing w/ Node Cluster API 2018-02-05 22:03:51 -08:00
Mars Hall
d88e7b287c Multi-process across all CPU cores 2018-02-05 21:51:32 -08:00
Mars Hall
e7a5eb4ca5 Update deployment runtime to Node 8.9.x (#16) 2018-02-05 21:10:11 -08:00
3 changed files with 36 additions and 17 deletions

View File

@@ -10,7 +10,7 @@ To deploy a frontend-only React app, use the static-site optimized
## Design Points
A combo of two npm projects, the backend server and the frontend UI. So there are two `package.json` configs.
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:
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
@@ -18,6 +18,7 @@ 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/)
* 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

View File

@@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "How to use create-react-app with a custom Node API on Heroku",
"engines": {
"node": "6.11.x"
"node": "8.9.x"
},
"scripts": {
"start": "node server",

View File

@@ -1,23 +1,41 @@
const express = require('express');
const path = require('path');
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const app = express();
const PORT = process.env.PORT || 5000;
// Priority serve any static files.
app.use(express.static(path.resolve(__dirname, '../react-ui/build')));
// Multi-process to utilize all CPU cores.
if (cluster.isMaster) {
console.error(`Node cluster master ${process.pid} is running`);
// Answer API requests.
app.get('/api', function (req, res) {
res.set('Content-Type', 'application/json');
res.send('{"message":"Hello from the custom server!"}');
});
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
// All remaining requests return the React app, so it can handle routing.
app.get('*', function(request, response) {
response.sendFile(path.resolve(__dirname, '../react-ui/build', 'index.html'));
});
cluster.on('exit', (worker, code, signal) => {
console.error(`Node cluster worker ${worker.process.pid} exited: code ${code}, signal ${signal}`);
});
app.listen(PORT, function () {
console.log(`Listening on port ${PORT}`);
});
} else {
const app = express();
// Priority serve any static files.
app.use(express.static(path.resolve(__dirname, '../react-ui/build')));
// Answer API requests.
app.get('/api', function (req, res) {
res.set('Content-Type', 'application/json');
res.send('{"message":"Hello from the custom server!"}');
});
// All remaining requests return the React app, so it can handle routing.
app.get('*', function(request, response) {
response.sendFile(path.resolve(__dirname, '../react-ui/build', 'index.html'));
});
app.listen(PORT, function () {
console.error(`Node cluster worker ${process.pid}: listening on port ${PORT}`);
});
}