Getting CORS error while debugging on mobile phone [closed]

  cors, express, javascript, node.js

I understand that this is a weird issue, but this happened to me.

When I visit the same website on my computer everything works fine with the code below, but when I try it on mobile phone I get a CORS error, can you please help me understand the issue?

require("dotenv").config();
const { PORT, NODE_ENV } = process.env;

const port = PORT || 5555;
const isProd = NODE_ENV === "production";

const cors = require("cors");
const express = require("express")();
const { nanoid } = require("nanoid");
const expressWs = require("express-ws");
const cookieParser = require("cookie-parser");

express.set("trust proxy", 1);
expressWs(express);
express.use(cors({ origin: true, credentials: true, optionsSuccessStatus: 200 }));
express.use(cookieParser());

const socket = require("./routes/socket");

// Used for generating an id and sending it to the user
express.get("/id", (req, res) => {
  const genId = nanoid(5);
  if (!req.cookies.id) {
    return res
      .cookie("id", genId, {
        httpOnly: isProd ? true : false,
        sameSite: isProd ? "none" : "lax",
        secure: isProd ? true : false
      })
      .json({ id: genId });
  }
});
express.use("/ws", socket);
// HTTP server
express.listen(port, () => console.log(`Server started at port ${port}`));

module.exports = express;

Update:

Same thing happens when connecting to phone-tethered internet.

Here’s what happens:

  • I connect to the website
  • The website should send a cookie
    • When that cookie exists the CORS error occurs

Source: Ask Javascript Questions

LEAVE A COMMENT