404 Not Found for static files in the public directory with Express.js on Vercel

34 views Asked by At

I have an Express.js application deployed on Vercel that serves static files from a public directory.

Despite the admin.html file being present in the directory, trying to access it via my vercel app results in a 404 Not Found error. (Just showing Cannot GET /index.html)

The application works as expected locally, but when deployed to Vercel, static files are not served correctly.

This is my data structure - it should be set up properly:

/
|-- server.js
|-- public/
    |-- admin.html
    |-- index.html
    |-- //other relevant html]

How i'm setting up my file serving:

const express = require('express');
const app = express();

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

// ...

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

My vercel.json config file:

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

My vercel logs aren't any help, just 404 - GET /

How can I make these files serve correctly?

0

There are 0 answers