currently im building a MERN stack project. Now i want to host my MERN in Vercel. The problem im encounter were, when i deploying it stuck at “Connected to MongoDB” and “Server is running on port 4000”. i suspect that it doesnt know what to do after that. But i dont know what the code should be. here is my server.js code
const express = require('express');
const mongoose = require('mongoose');
const studentRoute = require('./routes/Rstudent');
const staffRoute = require('./routes/Rstaff');
const userRoute = require('./routes/Ruser');
const cors = require('cors');
const app = express();
app.use(cors(
{
origin: ['my front end http'],
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true
}))
// app.use(cors(
// {
// origin: 'http://localhost:5173',
// methods: ['GET', 'POST', 'PUT', 'DELETE'],
// allowedHeaders: ['Content-Type', 'Authorization'],
// credentials: true
// }))
app.use(express.json());
app.use('/api/student', studentRoute);
app.use('/api/staff', staffRoute);
app.use('/api/user', userRoute);
//connect to mongoDB
mongoose.connect(process.env.MONGO_URI).then(() => {
console.log("connected to MongoDB");
//listen to request
const PORT = process.env.PORT || 4000; // Default port is 3000 if PORT variable is not provided
app.listen(PORT, () => {
console.log("Server is running on port", PORT);
});
}).catch(err => {
console.log("error:", err.message);
});
```.
This is the output in vercel terminal
Cloning completed: 999.94ms
Restored build cache
Running "vercel build"
Vercel CLI 34.1.3
Running "install" command: `npm install`...
up to date, audited 122 packages in 621ms
25 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
> [email protected] build
> node server.js
connected to MongoDB
Server is running on port 4000.
Then it just keep building