When trying to deploy a MERN stack app on Heroku, encountering issues with the front-end functionality not working as

The front-end is developed using create-react-app, while the backend is built with express, node.js, and MongoDB. It functions without any issues locally, but after deploying to Heroku, only the backend seems to be working...

index.js

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

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

package.json in the server side

  "scripts": {
    "start": "node index.js",
    "heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build"
  },

package.json in the client side

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:5000",

Furthermore, my mongoose setup on MongoLab is functioning properly and has been added to Heroku. Interestingly, when pushing to Heroku, everything appears to work seamlessly. The application navigates to the client-side, installs dependencies, and builds the package as expected.

Answer №1

After making the smart move of placing

app.use(express.static(path.join(__dirname, 'client', 'build')));
above the app.use(error) and error handling function, everything started working smoothly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

MongoDB MongooseError: The `collection.aggregate()` method cannot be called until the initial connection is fully established

Check out my personal website that I created using NextJS and hosted on Vercel. The site connects to a MongoDB database through Mongoose in my NodeJS API. Recently, I've been encountering a strange error affecting only about 1% of the users: Mongoose ...

utilizing nodejs' request.end() method prior to establishing the event listeners

According to the nodejs documentation found at http://nodejs.org/api/http.html#http_event_connect_1, there is a concern with the example code provided. In this code snippet, the request.end() function is called before setting up the listeners (req.on(...) ...

Encountered a problem when attempting to launch the React application

[Issue encountered while attempting to launch myfirstreact with npm start] Hello everyone, I am a beginner in the world of React. I am currently facing a problem when trying to run myfirstreact using npm start. Despite receiving an error message pointing ...

Ensure that agent.auth is executed prior to every test within a describe block

The following code is functioning correctly: describe('My App', function() { describe('when logged in', function() { it('should allow registered user to make a thing', function(done) { agent.post('/make-a-thi ...

Data loss occurs when the function malfunctions

Currently, I am working with Angular version 11. In my project, I am utilizing a function from a service to fetch data from an API and display it in a table that I created using the ng generate @angular/material:table command. Client Model export interfac ...

Refreshing access tokens in QuickBooks using Intuit's API - Upgrade your security with Oauth2

As I endeavor to obtain an access token from a refresh token via the OAuth2 of Intuit API (for Quickbook API access), I have successfully achieved this using axios in Node.js on my personal laptop. Unfortunately, when attempting to execute the script on ...

Nodemailer is experiencing difficulties when used within the routes directory

Recently, I encountered an issue with my subscribe form. It functions perfectly when called from app.js where the express server is defined. However, when I move subscribe.js to the routes folder and connect both files, it fails to send emails. Upon pressi ...

Access NodeJS libraries from different directories instead of the default "node_modules" folder without relying on relative paths

In my NodeJS project, I have two folders for libraries - one named "node_modules" which contains publicly available packages, and another called "somename_modules" where we keep proprietary libraries. It's common knowledge that when we use "import" o ...

What could be causing my node.js terminal to return duplicate responses for a single request?

// app.js code const express = require("express"); const app = express(); const bodyParser = require("body-parser"); app.set("view engine","ejs"); app.use(bodyParser.urlencoded({extended:true})) app.get("/",function(req,res){ res.render("home"); }); ...

There is no Method available when requiring Node.js/Express.js Middleware

Encountering an issue with requiring Express.js middleware in my code: ... var middlewares = require('./middlewares'); ... routes = require('./routes')(app, config, crypto, middlewares, models, oauth2); ... The above code snippet requ ...

Electron: Interactive menu customization

Is there a way in Electron to dynamically enable/disable specific MenuItem in the context menu based on the element that the user right-clicks on? Additionally, I am looking for a method to identify the exact element clicked and pass that information to th ...

Combining NodeJS and ExpressJS to deliver a unified JavaScript file when in production mode

Currently, I am managing multiple individual JS files in the following way: <script defer src="/js/libs/jquery.min.js"></script> <script defer src="/js/libs/plugins.js"></script> <!-- application core --> <script defer sr ...

Retrieve the HTML content of all children except for a specific child element in jQuery

Is there a way to utilize jQuery/Javascript for selecting the HTML content of the two <p> elements in the initial <div class="description? I'm open to using Regex as well. This specific jQuery selection is being executed within Node.js on a c ...

Why is the home page on my jade website showing a blank screen?

Currently, I am following a tutorial on Express, Mongo, and Jade. While I have successfully retrieved data from MongoDB, Jade is not rendering my page correctly. Click here for the tutorial. Here are the snippets of code I am using: app.js: app.get(&apo ...

When sending a form with a POST request in NODE, the data can be missing

I'm having trouble with setting up routes in an API and getting data from simple forms. Take a look at this basic HTML form: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>test form</titl ...

discovering the records with the closest value to the given parameter in mongodb using node.js

In my database, I have a collection named lights that consists of documents with the following structure: { "_id": "50eea4a53004cc6233d12b02", "Physicalentity": "Light", "Sensor": "Tinkerforge", "Unit": "Lux", "value": "47.2", "tim ...

Guide on setting up a MERN stack with a multi-tenant architecture

After successfully creating a full stack e-commerce website using React, Node, and MongoDB, I am now looking to deploy it with a multi-tenant architecture. Each store owner should have their own domain name. The Admin Dashboard will include various APIs s ...

Having trouble attaching handlers to a socket within a loop. Frustrating :/

In order to handle events sent to a socket in a more organized way, I have implemented a router. Within this router, my goal was to assign each module to a specific event. I mapped out event strings and their respective handlers within the "handlers" objec ...

Using fast refresh in Next.js is leading to a fetch error when integrated with Express.js

When trying to fetch data from my API, I encountered an error due to fast refresh in Next.js. It seems that req.params only work once because of a bug with Next.js's fast refresh feature. Do you know of any solutions or alternative ways to retrieve th ...

Why isn't the connect.use() function working in Node.js?

I have been studying and applying examples from a book to learn Node.js. While replicating one of the examples, which involved creating a middleware, I encountered an error when trying to run the JavaScript file. The error message stated "undefined is not ...