- Set
NODE_ENVtoproductionon Heroku withheroku config:set NODE_ENV=production - This sets the
NODE_ENVenvironment variable on Heroku - Install the
corspackage 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 initin the project root - This generates the
package.jsonin the project root - Heroku then recognizes the project as a Node.js project
- In the root
package.jsonscripts - Create a
startscript with a value ofnode server/app.js - Create a
postinstallscript 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
modetocorsfor fetch calls.
fetch(`${ domain }/api/v1/session`, {
method: 'DELETE',
mode: 'cors'
});
- If you need to include cookies, set
credentialstoinclude
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
--domainoption 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/CNAMEfile
- Or you can set the domain in a
- This deploys to surge
- Just remember to always run
npm run buildbefore deploying