Skip to content

Add Vercel serverless adapter to fix 404 deployment error#4

Draft
Copilot wants to merge 5 commits into
mainfrom
copilot/add-vercel-deployment-configuration
Draft

Add Vercel serverless adapter to fix 404 deployment error#4
Copilot wants to merge 5 commits into
mainfrom
copilot/add-vercel-deployment-configuration

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 4, 2025

Vercel returns 404 because the Express app calls app.listen() internally and doesn't export a compatible serverless entrypoint.

Changes

api/index.js - Serverless wrapper

  • Tries common paths: ../app, ../server, ../src/app, ../src/server, ./app
  • Validates module has Express methods (use, get)
  • Wraps with serverless-http or returns 500 with fix instructions if misconfigured

vercel.json - Platform configuration

  • Node 20.x runtime
  • Rewrites /(.*) → /api/index.js

PROPOSED_CHANGE.md - Maintainer guide

  • How to export app instance without listen()
  • Example server.js for local dev
  • Troubleshooting common deployment issues

.gitignore - Standard Node patterns

package.json - Added serverless-http@^3.2.0

Current State

// app.js - doesn't export the app
mongoose.connect(dbURI)
  .then((result) => app.listen(3000))  // ❌ Blocks serverless use
  .catch((err) => console.error(err));

Required Next Steps

See PROPOSED_CHANGE.md for restructuring:

  1. Export app instance: module.exports = app
  2. Move listen() to separate server.js for local dev
  3. Set environment variables in Vercel dashboard

The wrapper detects the current structure and returns actionable error messages until fixed.

Security: No vulnerabilities in dependencies, CodeQL clean.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • nodeproject.62tovra.mongodb.net
    • Triggering command: node -e try { const handler = require('./api/index.js'); console.log('✓ api/index.js loaded successfully'); console.log('✓ Export type:', typeof handler); } catch(e) { console.error('✗ Error loading api/index.js:', e.message); process.exit(1); } (dns block)
    • Triggering command: node -e const app = require('./app.js'); console.log('Type:', typeof app); console.log('Constructor:', app?.constructor?.name); console.log('Has listen:', typeof app?.listen); console.log('Is listening:', app?.listening); (dns block)
    • Triggering command: `node -e
      const handler = require('./api/index.js');
      console.log('Handler type:', typeof handler);
      console.log('Handler is function:', typeof handler === 'function');

// Simulate a request to see the error message
const mockReq = {};
const mockRes = {
status: (code) => {
console.log('Status code:', code);
return mockRes;
},
json: (data) => {
console.log('Response:', JSON.stringify(data, null, 2));
}
};

if (typeof handler === 'function') {
handler(mockReq, mockRes).catch(e => console.error('Handler error:', e.message));
}` (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Issue: Vercel deployment returns 404 (screenshot attached). Vercel likely can't find a build output or a compatible Node/Express entrypoint. The repository is an Express/EJS app and should be runnable on Vercel as a serverless function.

Goal: Create a pull request against main that makes the project deployable on Vercel by adding a serverless adapter and configuration. This PR will add new files and update package.json to include serverless-http as a dependency. It must not remove or break existing functionality; changes are additive and conservative.

Attached screenshot: image1

Files to add:

  1. api/index.js — serverless wrapper
  • Purpose: Wrap an exported Express app with serverless-http so Vercel can invoke it as a serverless function. The wrapper will attempt to require the Express app from common paths: ../app, ../server, ../src/app, ../src/server, ./app. If not found, the function will return a helpful JSON message explaining how to export the app.
  • Behavior: If the required module returns a server instance (i.e., called listen()), return a 500 JSON message instructing the maintainer to export the app instance instead.
  1. vercel.json — Vercel configuration
  • Purpose: Declare Node 18 runtime for api/index.js and rewrite all incoming routes to the function so client-side routes don't 404.
  • Content: functions entry for api/index.js with runtime nodejs18.x and a rewrites rule mapping '/(.*)' to '/api/index.js'.
  1. PROPOSED_CHANGE.md — explanation for maintainers
  • Purpose: Explain why files were added and how to adjust the server entrypoint if necessary (export app, do not call listen()). Include examples for app.js and server.js for local dev.

Files to modify:

  1. package.json — add dependency "serverless-http": "^3.0.0"
  • Preserve existing package.json content; only add this dependency under dependencies. If there is no dependencies object, create one without removing other fields.

Implementation notes / constraints:

  • The PR must be non-destructive: do not overwrite existing files other than updating package.json to add the dependency. If a file already exists at api/index.js or vercel.json, the PR should not clobber without caution — prefer to add new files; if conflicts exist, leave a clear note in the PR description for maintainers to resolve.
  • The wrapper should robustly try common entrypoints; if the project uses a different path, maintainers can modify api/index.js accordingly.
  • After merging, Vercel will install dependencies and redeploy. The maintainer should ensure their Express app exports the app instance (module.exports = app) and does not call app.listen() in the module.

Testing / QA instructions (to include in PR description):

  • Locally: npm install --save serverless-http and run vercel dev (Vercel CLI) to test the serverless function locally, or run local server with server.js using node server.js as described.
  • After merge: Monitor Vercel build and runtime logs for errors and paste them here for further debugging if needed.

Please create a new branch named add/vercel-serverless, add the files, update package.json, and open a PR to main titled: "Add Vercel serverless adapter, vercel.json, and serverless-http dependency to fix 404 on deployment" with the above description and the attached screenshot reference.

Do not merge: Leave the PR open for review.

This pull request was created as a result of the following prompt from Copilot chat.

Issue: Vercel deployment returns 404 (screenshot attached). Vercel likely can't find a build output or a compatible Node/Express entrypoint. The repository is an Express/EJS app and should be runnable on Vercel as a serverless function.

Goal: Create a pull request against main that makes the project deployable on Vercel by adding a serverless adapter and configuration. This PR will add new files and update package.json to include serverless-http as a dependency. It must not remove or break existing functionality; changes are additive and conservative.

Attached screenshot: image1

Files to add:

  1. api/index.js — serverless wrapper
  • Purpose: Wrap an exported Express app with serverless-http so Vercel can invoke it as a serverless function. The wrapper will attempt to require the Express app from common paths: ../app, ../server, ../src/app, ../src/server, ./app. If not found, the function will return a helpful JSON message explaining how to export the app.
  • Behavior: If the required module returns a server instance (i.e., called listen()), return a 500 JSON message instructing the maintainer to export the app instance instead.
  1. vercel.json — Vercel configuration
  • Purpose: Declare Node 18 runtime for api/index.js and rewrite all incoming routes to the function so client-side routes don't 404.
  • Content: functions entry for api/index.js with runtime nodejs18.x and a rewrites rule mapping '/(.*)' to '/api/index.js'.
  1. PROPOSED_CHANGE.md — explanation for maintainers
  • Purpose: Explain why files were added and how to adjust the server entrypoint if necessary (export app, do not call listen()). Include examples for app.js and server.js for local dev.

Files to modify:

  1. package.json — add dependency "serverless-http": "^3.0.0"
  • Preserve existing package.json content; only add this dependency under dependencies. If there is no dependencies object, create one without removing other fields.

Implementation notes / constraints:

  • The PR must be non-destructive: do not overwrite existing files other than updating package.json to add the dependency. If a file already exists at api/index.js or vercel.json, the PR should not clobber without caution — prefer to add new files; if conflicts exist, leave a clear note in the PR description for maintainers to resolve.
  • The wrapper should robustly try common entrypoints; if the project uses a different path, maintainers can modify api/index.js accordingly.
  • After merging, Vercel will install dependencies and redeploy. The maintainer should ensure their Express app exports the app instance (module.exports = app) and does not call app.listen() in the module.

Testing / QA instructions (to include in PR description):

  • Locally: npm install --save serverless-http and run vercel dev (Vercel CLI) to test the serverless function locally, or run local server with server.js using node server.js as described.
  • After merge: Monitor Vercel build and runtime logs for errors and paste them here for further debugging if needed.

Please create a new branch named add/vercel-serverless, add the files, update package.json, and open a PR to main titled: "Add Vercel serverless adapter, vercel.json, and serverless-http dependency to fix 404 on deployment" with the above description and the attached screenshot reference.

Do not merge: Leave the PR open for review.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@vercel
Copy link
Copy Markdown

vercel Bot commented Nov 4, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
blog-node Error Error Nov 4, 2025 6:58pm

Co-authored-by: Faizal-Malek <94194196+Faizal-Malek@users.noreply.github.com>
Co-authored-by: Faizal-Malek <94194196+Faizal-Malek@users.noreply.github.com>
Co-authored-by: Faizal-Malek <94194196+Faizal-Malek@users.noreply.github.com>
….js version

Co-authored-by: Faizal-Malek <94194196+Faizal-Malek@users.noreply.github.com>
Copilot AI changed the title [WIP] Add serverless adapter and configuration for Vercel deployment Add Vercel serverless adapter to fix 404 deployment error Nov 4, 2025
Copilot AI requested a review from Faizal-Malek November 4, 2025 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants