Page 4: Deploying Express.js API to Vercel (Serverless)

4. Deploying Express.js API to Vercel (Serverless)

Vercel can deploy your Express.js application as serverless functions. This requires a vercel.json configuration file at the root of your project.

4.1. Create vercel.json

At the root of your monorepo (e.g., my-fullstack-app/vercel.json), create a vercel.json file to tell Vercel how to handle your API routes.

// vercel.json
{
  "rewrites": [
    {
      "source": "/api/(.*)",
      "destination": "/api/"
    }
  ],
  "functions": {
    "api/index.js": {
      "runtime": "node tolerant",
      "memory": 1024,
      "maxDuration": 10
    }
  }
}

Explanation:

4.2. Ensuring Correct Dependencies

Vercel will install dependencies for both your Next.js app and your api/ directory. Make sure each package.json correctly lists its dependencies.

For example, express and cors should be in api/package.json's dependencies.

4.3. Deploy and Test

Push your changes to your Git repository. Vercel will detect the vercel.json file and deploy your Express.js API as serverless functions alongside your Next.js app.

After deployment, you should be able to access your API at https://your-vercel-domain.vercel.app/api/hello.