19
loading...
This website collects cookies to deliver better user experience
hello-world
with your app name) :$ npx create-react-app hello-world
$ cd hello-world
hello-world
repository in your preferred text editor. If you’re using atom, simply type atom .
in the terminal to open your repo. Here is what the repo will look like:hello-world
├── README.md
├── node_modules
├── package.json
├── .gitignore
├── public
│ ├── favicon.ico
│ ├── index.html
│ └── manifest.json
└── src
├── App.css
├── App.js
├── App.test.js
├── index.css
├── index.js
├── logo.svg
└── registerServiceWorker.js
touch server.js
//server.js
const express = require('express');
const favicon = require('express-favicon');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
app.use(favicon(__dirname + '/build/favicon.ico'));
// the __dirname is the current directory from where the script is running
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, 'build')));
app.get('/ping', function (req, res) {
return res.send('pong');
});
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(port);
express
, express-favicon
, and path
to your dependencies:yarn add express express-favicon path
package.json
file, change the start
script to the following:start: "node server.js"
yarn build
yarn start
to start the production server locally..env
:touch .env
.env
file to prevent source maps from being generated.#.env
GENERATE_SOURCEMAP=false
trouble
debugging an issue in production but you want to keep your source code private, you can create a separate branch, remove that line from the .env
file, and deploy that branch to a secret heroku url.heroku login
heroku create sample-react-production-app
yarn install
git add .
git commit -m "initial commit"
heroku git:remote -a sample-react-production-app
git push heroku master
heroku create [app-name]
, then you don’t need to run heroku git:remote -a [app-name]
.heroku open