expressvercel

Vercel + Express: Can't hit any endpoint but root


I have an express app set up and running fine locally, but on Vercel it will deploy successfully, but only the Hello World! of the / will show, while all other endpoints will 500 error on me.

So for example:

index.js

// This works on my Vercel
index.get('/', (req, res) => {
   res.send('Hello World!')
})

// This would not
index.post('/someEndpoint', (req, res) =>  {
   some code here....
})

This is my vercel.json (yes, I have read the Vercel Express documentation, it is unhelpful)

{
  "version": 2,
  "builds": [
    {
      "src": "./index.js",
      "use": "@vercel/node"
    }
  ],
  "rewrites": [
    {
      "source": "/(.*)",
      "destination": "/"
    }
  ]
}

I have also added this to my index.js:

const corsOptions = {
    "origin": "*",
    "methods": "GET, HEAD, PUT, PATCH, POST, DELETE",
    // other options
}

index.use(cors(corsOptions));

I have spent hours on this now googling every nook and cranny I can find, but no go. Any help would be appreciated!


Solution

  • Wow, after several days, I finally figured it out.

    It all came down to my database. I am using MongoDB and I had not whitelisted the IP for Vercel. Which apparently they use a dynamic IP Address. So they have a Vercel + MongoDB integration that sends data along for this very purpose.

    Set up the integration, refreshed my app, and no more issues!


    I found this by hitting certain endpoints with Postman and then looking at the logs on Vercel found here:

    Vercel Logs