0
votes

I have an express server that deals with serving a react app which is in a "build" folder in the same directory as the server.

The server also deals with setting up api endpoints.

My problem is that every endpoint ends up serving the react app.

Server.js

const express = require("express");
const cors = require("cors");

const { PrismaClient } = require("@prisma/client");

const port = process.env.PORT || 3001;

const app = express();
app.use(express.json());
app.use(cors());

const prisma = new PrismaClient();

let current_data = [];

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


app.get("/api/", (req, res) => {
  res.json(current_data);
});

app.get("/", function (req, res) {
  res.sendFile(path.join(__dirname, "build", "index.html"));
});

app.listen(port, () => {
  console.log("connected.");
});

Thank you

Have you tried removing the res.sendFile() from the / route? Since you're serving your build folder, you can simply access localhost:<port>/index.html and it should display your React App. - Ricardo Passos