Home > other >  Heroku cannot GET / due to 'failed to load resource: server responded with status of 404'
Heroku cannot GET / due to 'failed to load resource: server responded with status of 404'

Time:09-23

Works locally on node server.js

Image of folder

server.js code:

const express = require('express');
const path = require('path');
const db = require('./db/db.json'); //*db = database
const PORT = process.env.PORT || 3001;
const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use(express.static('public'));

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

//TEXT EDIT: this is at the bottom of the server.js
app.listen(PORT, () => {
    console.log(`Port is live http://localhost:3001`)
})

package.json:

{
  "dependencies": {
    "express": "^4.18.1"
  },
  "scripts": {
    "start": "node Develop/server.js"
  }
}

Have spent close to 4 hours on stack overflow trying all the solutions proposed on similar questions.

heroku logs

CodePudding user response:

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

changed to:

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

now getting a different error but at least it's something new

Resolved the other error so hopefully someone that has been having the same issue as me can see this. The below code

app.use(express.static('public'));

For some reason, does not work with heroku, but does whenever launching the server with node.js locally. I think this is because I have my server.js file inside the 'develop' folder? Not too sure, but I ended up changing that code to

app.use(express.static(__dirname   '/public'));

which works

  • Related