Search code examples
node.jsexpressserverlessvercel

Vercel + Express, can't seem to get any complex paths working


I've been trying to get a GraphQL API up and running on Vercel using Express. I've managed to get my main API working under the /api endpoint without many problems by doing this:

app.use(
  '/api',
  graphqlHTTP(async (req, res) => ({
    schema,
    context: createContext({ req, res }),
    graphiql: true,
  })),
)

This exposes the API as well as a graphql playground without any problems

So I then tried do expand it with other non-graphql endpoints for OAuth purposes, but no matter what I do I can't seem to access those endpoints once the API is deployed.

If I try to register:

app.get('/api/testing', (req, res) => res.json({ foo: 'bar' }))

I will get the correct response when developing locally, but once it's deployed I get a 404.

I've made the repository public, the server file can be found on Github.

Anyone know what's going on? Why aren't any endpoints but the one located at /api working?


Solution

  • It's on this page about using Express with Vercel. You have to add vercel.json to the root of your project with this content:

    {
      "rewrites": [{ "source": "/api/(.*)", "destination": "/api" }]
    }
    

    then you can use:

    app.get("/api/some/path", (req, res) => {
      res.send("wow, it actually worked");
    });