I have deployed a express server which works with a react app and its a stock market project. i have one repo with two folders as frontend and server and both are deployed separately. The project works perfectly fine with local environment and frontend also works if express server is working locally. I am facing cors issue with error as
login:1 Access to XMLHttpRequest at 'https://sbroker-backend.vercel.app/user/login' from origin 'https://sbroker.vercel.app' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
When i try to make similar request on postman i get “FUNCTION_INVOCATION_FAILED” error. It would be really helpful if anyone could help resolve this error.
const express = require("express");
const app = express();
const userroutes = require("../routes/routelogin");
const profileroutes = require("../routes/routeprofile");
const stockroutes = require("../routes/routestock");
const watchlistroute = require("../routes/routewatchlist");
const database = require("../config/database");
const cookieparser = require("cookie-parser");
const cors = require("cors");
const dotenv = require("dotenv");
const morgan = require("morgan"); // Logging middleware
const login =require("../controllers/login");
// app.use(cors({
// origin: 'http://localhost:3000',
// methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
// credentials: true,
// optionsSuccessStatus: 204,
// }));
app.use(
cors({
origin:["http://localhost:3000","https://sbroker.vercel.app"],
credentials:true,
})
)
dotenv.config();
const PORT = process.env.PORT || 4000;
// Connect to the database
database.connect();
app.use(express.json());
app.use(cookieparser());
// Routes
app.use("/user", userroutes);
/*app.post("/user/login",(req,res)=>{
return res.json({
success:true,
message:"login wala chl rha"
});
});
*/
//app.post("/user/login",login.login);
app.use("/profile", profileroutes);
app.use("/stock", stockroutes);
app.use("/watchlist", watchlistroute);
// Root route
app.get("/", (req, res) => {
return res.json({
success: true,
message: "Your server is up and running....",
});
});
// Handle 404 errors
// Error handling middleware
// app.use((err, req, res, next) => {
// console.error("Server error:", err.stack || err.message);
// res.status(500).json({
// success: false,
// message: "An unexpected error occurred",
// error: process.env.NODE_ENV === "development" ? err.message : undefined,
// });
// });
// Start the server
app.listen(PORT, () => {
console.log(`App is running on http://localhost:${PORT}`);
});
Above is index.js.
{ "version": 2, "rewrites": [{ "source": "/(.*)", "destination": "/api" }] }
Above is vercel.json which i took from vercel’s post about express js deployment.
Repo Link
this is the repo link.
frontend link
this is the frontend link
backend link
this is the backend link.
(In frontend code, only login code has backend url in request because without it other things cant be accessed anyways. other components have url of local host)