最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - POST request works via Hoppscotch but not from the frontend in my MERN-STACK-APP - Stack Overflow

programmeradmin2浏览0评论

I’m working on a MERN stack app where users can add, update, delete, and fetch tasks. The backend is set up using Express, and the frontend is built using React. All routes work fine when tested using Hoppscotch (or Postman). However, the POST request to add a task does not work from the frontend, even though update, delete, and other operations work perfectly. I hosted backend in render and frontend in vercel.

const app = express();

const corsOptions = {
  origin: ";,
  methods: ["GET", "POST", "PUT", "DELETE", "PATCH"],
  credentials: true,
};

app.use(cors(corsOptions));

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

app.use(
  "/tasks",
  (req, res, next) => {
    let userID = req.cookies.userID;
    if (!userID) {
      userID = uuidv4();
      res.cookie("userID", userID, {
        httpOnly: false,
        maxAge: 5 * 24 * 60 * 60 * 1000,
        sameSite: "none",
        secure: true,
      });
    }
    next();
  },
  taskRoutes
);

connectDB();

module.exports = app;

I also modified CORS settings, still not working. Why does the POST request work from Hoppscotch but fail from the frontend? Could this be related to CORS, cookies, or how the request is being made? Any guidance would be greatly appreciated!

These are the images of post, get request from hoppscotch and put,delete,patch request from frontend.

enter image description here enter image description here enter image description here

enter image description here SET-COOKIES not showing from get request in frontend

HEADER

access-control-allow-credentials:  true 
access-control-allow-origin:   
alt-svc:  h3=":443"; ma=86400 
cf-cache-status:  DYNAMIC 
cf-ray:  90476141fd6847b8-BOM 
content-encoding:  br 
content-length:  261 
content-type:  application/json; charset=utf-8 
date:  Sun, 19 Jan 2025 14:05:29 GMT 
etag:  W/ "273-TcZ4w0lpDjLAUMVoCDj+Jmi/oqQ" 
priority:  u=1,i 
rndr-id:  6a14a3d6-4ebc-468c 
server:  cloudflare 
server-timing:  cfExtPri 
vary:  Origin, Accept-Encoding 
x-powered-by:  Express 
x-render-origin-server:  Render
发布评论

评论列表(0)

  1. 暂无评论