Search code examples
node.jsexpresscreate-react-appvercel

Route handlers not working properly in express server (Create react app)


I have a simple react app made with CRA, and I deployed it on vercel. Everything seems to be working with one exception. My route handlers are not sending back the correct response. It seems like they are sending the index.html file that is created from the build script. I'm not really sure why this is happening. It sent the correct response when I built it locally. Here is the code for the express server:

    const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();

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

app.get("/ping", function (req, res) {
  return res.send("pong");
});

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

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server listening on ${PORT}`);
});

And here is the file structure if that helps:

enter image description here


Solution

  • I figured out the issue. Vercel only supports serverless deployment. You can't host an express server, just a static site