Im trying to deploy the backend of my MERN app and I keep having trouble. I've tried to deploy to Vercel as well as Railway and both failed so I know its something in my code not allowing it to deploy.
In a local environment everything works like its suppose to. I can connect to my mongodb and everything runs as it should.
This is my first time trying to deploy a MERN app so I've done all I know to do.
Here is the structure of my backend folder:

These are the files that could be causing the issue:
index.js:
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const campgroundRoutes = require("./routes/campgrounds");
const userRoutes = require("./routes/users");
// express app
const app = express();
const PORT = process.env.PORT || 3000;
//middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(
cors({
origin: "*", // Replace with the actual origin of your frontend
credentials: true,
})
);
const dbUrl = process.env.DB_URL;
// connect to db
mongoose.connect(dbUrl);
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("Connected to MongoDB");
});
//routes
app.use("/campgrounds", campgroundRoutes);
app.use("/api/users", userRoutes);
app.listen(PORT, () => {
console.log(`Serving at http://localhost:${PORT}`);
});
routes/users.js:
const express = require("express");
const router = express.Router();
const { registerUser, loginUser } = require("../controllers/users");
// register route
router.post("/register", registerUser);
// login route
router.post("/login", loginUser);
module.exports = router;
routes/campgrounds.js:
const express = require("express");
const router = express.Router();
const campgrounds = require("../controllers/campgrounds");
const multer = require("multer");
const { storage } = require("../cloudinary");
const upload = multer({ storage });
router
.route("/")
.get(campgrounds.getAllCampgrounds)
.post(upload.array("images", 4), campgrounds.createCampground);
router
.route("/:id")
.get(campgrounds.getCampgroundById)
.put(upload.array("images", 4), campgrounds.updateCampground)
.delete(campgrounds.deleteCampground);
module.exports = router;
package.json:
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon index.js",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.1",
"cloudinary": "^1.41.0",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.2",
"mongodb": "^6.2.0",
"mongoose": "^8.0.0",
"multer": "^1.4.5-lts.1",
"multer-storage-cloudinary": "^4.0.0",
"validator": "^13.11.0"
},
"devDependencies": {
"nodemon": "^3.0.3"
}
}
I really don't know what else to do so if anyone could please help I would greatly appreciate it.