0

I'm trying to deploy an Express.js + MongoDB + Mongoose backend to Vercel, but I’m getting this error when accessing any route (including /api/v1):

404: NOT_FOUND  
Code: NOT_FOUND

Project structure:

Backend/
├── src/
│   ├── app.js
│   ├── index.js
│   ├── routers/
│   │   ├── user.route.js
│   │   ├── company.route.js
│   │   └── ... (other routes)
├── vercel.json
├── package.json
└── ...

vercel.json:

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

app.js:

import express from "express";
import cookieParser from "cookie-parser";
import cors from "cors";
import dotenv from "dotenv";
import indexRoutes from "./routers/index.routes.js";
import userRoutes from "./routers/user.route.js";  
import companyRoute from "./routers/company.route.js";
import jobRoute from "./routers/job.route.js";
import applicationRoute from "./routers/application.route.js";
import adminRoute from "./routers/admin.route.js";
import interviewRoute from "./routers/interview.route.js";
import portfolioRoute from "./routers/portfolio.route.js";

dotenv.config();

const app = express();

// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());

const corsOptions = {
    origin: "http://localhost:5173",
    credentials: true,
    methods: ["GET", "POST", "PUT", "DELETE"],
    allowedHeaders: ["Content-Type", "Authorization"],
};
app.use(cors(corsOptions));

// Test Route
app.get("/", (req, res) => {
    return res.status(200).json({
        message: "Connected to the server",
        success: true
    });
});

// Routes
app.use("/api/v1/user", userRoutes);
app.use("/api/v1/company", companyRoute);
app.use("/api/v1/job", jobRoute);
app.use("/api/v1/application", applicationRoute);
app.use("/api/v1/admin", adminRoute);
app.use("/api/v1/interviews", interviewRoute);
app.use("/api/v1/portfolio", portfolioRoute);

export default app;

When deployed to Vercel:

  • Accessing https://smartjobportal-server.vercel.app/api/v1 (or any route) gives 404 NOT_FOUND.
  • Even root / route returns 404.
  • Logs show no output, no crash, nothing.

What I’ve Tried:

  1. Confirmed all routes are properly defined.
  2. MongoDB connection works locally.
  3. app.listen() works when running npm run dev or node src/index.js.
  4. Added a test / route to verify.
  5. Deployed with Vercel CLI (successful build and deploy).
  6. vercel.json correctly configured to point to src/index.js.

How can I fix this 404 error and get my Express routes working on Vercel?


2 Answers 2

0

i think that must be the configs in your vercel.json

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

In the documentation https://vercel.com/guides/using-express-with-vercel config:

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

But the "destination" in your code must me "destination": "/routers" , (I think).

Take a look at the documentation, I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't recommend you to deploy your express.js backend on vercel.
Instead try Railway.
It's fast, clean and backend specific.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.