Vercel not showing expressjs static files

Viewed 295

So I have an Expressjs app that serves 2 purposes API and the website itself which is React

I tried to deploy it, but it only loads the metatags from React but not the page content which is weird.

Here is the code:

process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 1;
require("dotenv").config();
const express = require("express");
const app = express();

const path = require("path");
const logger = require("morgan");
const cors = require("cors");

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());

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

app.get("*", function (_, res) {
  res.sendFile(
    path.join(__dirname, "./admin/build/index.html"),
    function (err) {
      if (err) {
        res.status(500).send(err);
      }
    }
  );
});
app.use("/api", require("./routes"));

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

module.exports = app;

Here is vercel.json:

{
  "version": 2,
  "builds": [
    {
      "src": "./index.js",
      "use": "@vercel/node"
    },
    {
      "src": "./admin/build",
      "use": "@vercel/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "/"
    }
  ]
}

Am I doing anything wrong or forgetting something?

0 Answers
Related