- Set
NODE_ENV
toproduction
on Heroku withheroku config:set NODE_ENV=production
- This sets the
NODE_ENV
environment variable on Heroku - Install the
cors
package on your express server - This is the setup if you're accepting credentials:
// ----------------------------------------
// CORS
// ----------------------------------------
const cors = require('cors');
app.use(cors({
origin: process.env.NODE_ENV === 'production' ? 'https://frantic-government.surge.sh' : 'http://localhost:3000',
credentials: true
}));
- If not, I believe you can just do:
const cors = require('cors');
app.use(cors();
Or
const cors = require('cors');
app.use(cors({ origin: '*' });
- Assuming that your express app is in
./server/
directory - You first run
npm init
in the project root - This generates the
package.json
in the project root - Heroku then recognizes the project as a Node.js project
- In the root
package.json
scripts - Create a
start
script with a value ofnode server/app.js
- Create a
postinstall
script with a value ofcd server && npm install --save
- This installs the dependencies and runs the express app
- Must have Heroku Toolbelt
- Run
heroku create
- Run
git add -A
,git commit
,git push -u origin master
,git push -u heroku master
- Are we in production? Use helper file to determine domain for fetch calls
const isProduction = () => {
return !window.location.href.match('localhost');
};
const domain = isProduction() ?
'https://peaceful-mesa-81349.herokuapp.com' :
'http://localhost:3001';
- You want to set
mode
tocors
for fetch calls.
fetch(`${ domain }/api/v1/session`, {
method: 'DELETE',
mode: 'cors'
});
- If you need to include cookies, set
credentials
toinclude
fetch(`${ domain }/api/v1/session`, {
method: 'DELETE',
credentials: 'include',
mode: 'cors'
});
- Install surge with NPM
npm install --global surge
- The first time you deploy to surge it will create a domain for you
- After that you must specify it with the
--domain
option or use a CNAME file.- Custom domain: http://surge.sh/help/adding-a-custom-domain
- CNAME file: http://surge.sh/help/remembering-a-domain
- In your
client/
folder, runnpm run build
- Always commit changes before deploying,
git add -A
,git commit
,git push -u origin master
- Assuming you're in the
client/
directory runsurge --domain yourdomain.surge.sh build
- Or you can set the domain in a
client/public/CNAME
file
- Or you can set the domain in a
- This deploys to surge
- Just remember to always run
npm run build
before deploying