0

I'm currently trying to deploy my MERN application's backend on Vercel.

Before this try, I learned to deploy the MERN site on Vercel by small todo project. I work fine now and I do the same for the following project. but it shows a 404 error

This is my file structure

enter image description here

This is my package.json

{
    "name": "backend",
    "version": "1.0.0",
    "description": "simple mern note taking application for open ended assignment",
    "main": "server.js",
    "engines": {
        "node": "20.x"
    },
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "dev": "nodemon server.js"
    },
    "author": "baos sora",
    "license": "ISC",
    "dependencies": {
        "cors": "^2.8.5",
        "dotenv": "^16.4.5",
        "express": "^4.18.3",
        "mongodb": "^6.5.0",
        "mongoose": "^8.2.3",
        "node-cron": "^3.0.3",
        "nodemon": "^3.1.0"
    }
}

this is my vercel.json

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

this is my env

URL= worked url from mongodb
PORT=3000

This is my server.js

//import required modules
const express = require('express');
require('dotenv').config();
const mongoose = require('mongoose');
const cors = require('cors');

const NoteRoutes = require('./routes/noteRoutes')


const { getCategory, deleteCategory, getOneCategryData } = require('./controller/categoryController')
const { getDynamicNoteData, DeleteInActiveNotes, AutoHardDeleteNotes } = require('./controller/noteController')

//initialize express app
const app = express();

// Middleware to parse incoming requests with JSON
app.use(express.json())

//middleware to handle Cors policy
app.use(cors());


app.get('/', (req, res) => {
    res.json({ message: 'Welcome to Note API' })
})

app.use('/note', NoteRoutes)


//manually create routes because of casting issues
app.get('/search', getDynamicNoteData)
app.delete('/remove', DeleteInActiveNotes)
app.get('/category', getCategory)
app.get('/category/:id', getOneCategryData)
app.delete('/category/:id', deleteCategory)


const PORT = process.env.PORT || 5000;
const URL = process.env.URL;


//establish the connection to MongoDB  database using Mongoose
mongoose.connect(URL)
    //if the connection is successful display the success messages
    .then(() => {
        console.log("Connected with MongoDB");

        //call auto deletion function
        AutoHardDeleteNotes()

        //app listening port
        app.listen(PORT, () => {
            console.log("Server is running on port " + PORT);
        })
    })
    //if there is an error on connecting show display the error message
    .catch(err => {
        console.log("Application has Following Errors:  \n" + err);
    })

This shows when I deploy the project

enter image description here

Please help me find this issue. this is my internship's 2nd round open-ended assignment the deadline ends tomorrow. Until today I only used Netlify. but their guideline said need to add in Vercel.

3
  • before deploy, have to run "npm build"? Commented Mar 29, 2024 at 4:36
  • no.i not run that npm build..should i do it before Commented Mar 29, 2024 at 10:43
  • Before deployment on vercel must run this command ... If there is any error it returns file name Wich saves time or make easy for debugging. Commented Mar 30, 2024 at 20:32

2 Answers 2

3

According to the Vercel guide for deploying an Express server.

 Create a directory named /api.
 Inside /api, create a file named index.ts. This will be your main server file.

You need to create an /api folder at the root level and rename your server.js to index.js, then move the file to the /api folder.

Hope this helps!

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

2 Comments

now bakend work fine. but after the change of server.js to index.js when i deployed the fronend, it not load the data to frontend Error Occured: AxiosError: Network Error
Can you share the code where you call the backend?
1

just rename index.js instead of server.js

Comments

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.