Skip to content

Commit

Permalink
Trying a different approach hosting on heroku with node
Browse files Browse the repository at this point in the history
  • Loading branch information
aschokking committed Sep 30, 2018
1 parent 0778697 commit 2780fe4
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 32 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ hs_err_pid*

.gradle
.classpath
#/build
bin

# dependencies
Expand All @@ -34,7 +33,7 @@ bin
/coverage

# production
#/build
/build

# misc
.DS_Store
Expand Down
7 changes: 0 additions & 7 deletions JSVisualizer/build/asset-manifest.json

This file was deleted.

Binary file removed JSVisualizer/build/favicon.ico
Binary file not shown.
1 change: 0 additions & 1 deletion JSVisualizer/build/index.html

This file was deleted.

15 changes: 0 additions & 15 deletions JSVisualizer/build/manifest.json

This file was deleted.

1 change: 0 additions & 1 deletion JSVisualizer/build/service-worker.js

This file was deleted.

2 changes: 0 additions & 2 deletions JSVisualizer/build/static/css/main.e1a77369.css

This file was deleted.

1 change: 0 additions & 1 deletion JSVisualizer/build/static/css/main.e1a77369.css.map

This file was deleted.

2 changes: 0 additions & 2 deletions JSVisualizer/build/static/js/main.a5997380.js

This file was deleted.

1 change: 0 additions & 1 deletion JSVisualizer/build/static/js/main.a5997380.js.map

This file was deleted.

Binary file removed JSVisualizer/build/static/media/field.1c3bc215.jpg
Binary file not shown.
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "heroku-cra-node",
"version": "1.0.0",
"description": "How to use create-react-app with a custom Node API on Heroku",
"engines": {
"node": "8.9.x"
},
"scripts": {
"start": "node server",
"heroku-postbuild": "cd JSVisualizer/ && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
},
"cacheDirectories": [
"node_modules",
"JSVisualizer/node_modules"
],
"dependencies": {
"express": "^4.16.3"
},
"repository": {
"type": "git",
"url": "https://github.com/Team488/SimpleVisualizer.git"
},
"keywords": [
"node",
"heroku",
"create-react-app",
"react"
],
"license": "MIT",
"devDependencies": {}
}
41 changes: 41 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const express = require('express');
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();

// Priority serve any static files.
app.use(express.static(path.resolve(__dirname, '../JSVisualizer/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, '../JSVisualizer/build', 'index.html'));
});

app.listen(PORT, function () {
console.error(`Node cluster worker ${process.pid}: listening on port ${PORT}`);
});
}

0 comments on commit 2780fe4

Please sign in to comment.