question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot get route when deployed to heroku

See original GitHub issue

Your config has been working great so far locally. When i deployed it to Heroku i started getting the following issue:

If i go to https://enjoy-copenhagen-dev.herokuapp.com everything is fine, i can navigate through the app, no issues there.

If i go to https://enjoy-copenhagen-dev.herokuapp.com/api or whichever one of my api post and get endpoints everything works.

If i try to open any other route i get Cannot GET /route-i-tried-to-open. image

My server.js

const cors = require('cors');
const path = require('path');
const express = require('express');
const nodemailer = require('nodemailer');
const bodyParser = require('body-parser');
const app = express();

const mail = require('./emailSenders');

app.use(cors());

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'DELETE, PUT, GET, POST');
  res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept'
  );
  next();
});

// support json encoded bodies
app.use(bodyParser.json());
// support encoded bodies
app.use(bodyParser.urlencoded({ extended: false }));

app.set('port', process.env.PORT || 3001);

// Express only serves static assets in production
if (process.env.NODE_ENV === 'production') {
  app.use(express.static('client/build'));
}

app.get('/api', (req, res) => {});
app.post('/api/createCustomer', (req, res) => {});
app.post('/api/retrieveCustomer', (req, res) => {});
app.post('/api/createCharge', (req, res) => {});
app.post('/api/sendEmail', (req, res) => {});

app.listen(app.get('port'), () => {
  console.log(`Find the server at: http://localhost:${app.get('port')}/`); // eslint-disable-line no-console
});

package.json

{
	"name": "lookup-server",
	"version": "0.0.1",
	"private": true,
	"dependencies": {
		"babel-core": "6.14.0",
		"body-parser": "^1.18.2",
		"cors": "^2.8.4",
		"ejs": "^2.5.7",
		"email-templates": "^3.5.2",
		"express": "4.13.3",
		"fs": "0.0.2",
		"node-schedule": "^1.3.0",
		"nodemailer": "^4.4.2",
		"sql.js": "0.3.2",
		"stripe": "^5.4.0"
	},
	"scripts": {
		"start": "concurrently \"npm run server\" \"npm run client\"",
		"server": "node server.js",
		"client": "node start-client.js",
		"dev": "echo \"This command has been deprecated. Use 'npm start'\" && exit 1",
		"lint": "eslint ."
	},
	"devDependencies": {
		"concurrently": "3.1.0",
		"eslint": "^3.19.0",
		"eslint-config-airbnb": "14.1.0",
		"eslint-plugin-import": "2.2.0",
		"eslint-plugin-jsx-a11y": "4.0.0",
		"eslint-plugin-react": "6.9.0"
	}
}

file structure image

Please help me fix this!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:17

github_iconTop GitHub Comments

31reactions
borisyordanovcommented, Mar 21, 2018

The solution is to make sure you have this in your server:

if (process.env.NODE_ENV === 'production') {
	app.use(express.static('client/build'));
}

And also add this:

app.get('*', (request, response) => {
	response.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});

You can’t serve a static single page application with dynamic routes without an express server set up this way.

5reactions
jitto21commented, Feb 14, 2020

@sudhaparayil Try this:

if(process.env.NODE_ENV === 'production'){
    //set static folder
    app.use(express.static('client/build'));
}
app.get('*',(req, res) => {
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});

What is ‘client’ and ‘build’ ??

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router cannot GET /route only after deployed to Heroku
This basically means "for any route not already matched, send the index.html file", which will then load your webapp, which in turn will...
Read more >
Troubleshooting Node.js Deploys - Heroku Dev Center
Your Node.js deploy failed - now what? Start with these simple steps to troubleshoot a build issue. Check the buildpack.
Read more >
MERN deploy to heroku Cannot Get
First things first, you need to have the IP of your heroku app. If this is a free tier app, you may be...
Read more >
Cannot get route when deployed to heroku : r/node - Reddit
Cannot get route when deployed to heroku. Hello everyone,. I'm pretty new to Nodejs and this is probably a very noobish mistake.
Read more >
Deploying an Express Node.js Backend on Heroku
But sometimes, when it comes to getting it on the Web, you have a lot to consider. After making your Node.js app work...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found