-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trying a different approach hosting on heroku with node
- Loading branch information
1 parent
0778697
commit 2780fe4
Showing
13 changed files
with
73 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
} |