Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heroku app returns Not Found when going to specific url Route #4132

Closed
muigaiunaka opened this issue Mar 9, 2018 · 2 comments
Closed

Heroku app returns Not Found when going to specific url Route #4132

muigaiunaka opened this issue Mar 9, 2018 · 2 comments

Comments

@muigaiunaka
Copy link

muigaiunaka commented Mar 9, 2018

Hello !
I was hoping to receive help with an issue I am having after deploying my app to Heroku. I built my front end with Heroku and my server side code uses Express and node. When testing locally, I can access a specific route, for example localhost:3000/post and it loads as expected. When I go to my site at https://treasury-of-weary-souls.herokuapp.com/post , the page returns Not Found.

My basic folder setup is like so:
project/
---- app/
-------- src/
---- backend/
-------- index.js

Here's what I have in my server side code:

// GLOBAL VARIABLES
const express = require('express');
const path = require("path");
const bodyParser = require("body-parser");
const morg = require("morgan");
const States = require('./states.js');
const stringify = require("json-stringify-pretty-compact")

const PORT = process.env.PORT || 5001;
const app = express();

// logging for request to the console
app.use(morg("dev"));

// Configure body parser for AJAX requests
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
    app.get('/map', (req, res) => {
        res.sendFile(path.join(__dirname + '/app/build/index.html'));
    });
    app.get('/post', (req, res) => {
        res.sendFile(path.join(__dirname + '/app/build/index.html'));
    });
    app.use('/', express.static("app/build"));
    app.use(express.static(path.join(__dirname, 'app/build')));
    // app.get('/*', function (req, res) {
    //     res.sendFile(path.join(__dirname, 'build', 'index.html'));
    // });
}
app.get('*', (req, res) => {
     res.sendFile(path.join(__dirname + '/app/build/index.html'));
});

app.listen(PORT);

console.log(`App listening on ${PORT}`);
@jamesmosier
Copy link

If this helps, here is what I have using CRA & Express:

Project structure:

client
-- build
-- public
-- src
-- ...other CRA files
server
-- app.js

and then inside app.js (my Express server):

app.use(express.static(path.join(__dirname, '/../client/build')));

app.use('/api/some_api_endpoint', oneOfMyRoutes);

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

so you'll see, I have to go up a directory in order to reach my client/build folder (see: __dirname + '/../client/build/index.html').

@Timer
Copy link
Contributor

Timer commented Apr 15, 2018

Hi there! I'm sorry to say, but we don't have enough time to help with general-purpose questions.

You can find a wealth of information and people willing to help in numerous React Communities.

Thanks!

Visiting your site looks like you have it resolved, though.

@Timer Timer closed this as completed Apr 15, 2018
@lock lock bot locked and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants