CORS issue with Node.js w/Express & Socket.io backend hosted on Render

Complete web dev newbie here. Trying to make my first learning project. My front end is made with Vue.js and hosted on Netlify. My backend is made with Node.js, Express and Socket.io. Currently, the backend is deployed with Render. Everything worked perfectly locally, but broke when I deployed stuff. right now it’s basically a bad chat app, with plans to turn it into more once I figure out how to actually deploy.

I’ve redacted the urls, or made them generic, but can confirm the URLs are accurate. This is the error:

Access to XMLHttpRequest at 'https://**redacted**.onrender.com/socket.io/?EIO=4&transport=polling&t=OysBd2j' from origin 'https://**subdomain.mydomain.ca' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have tried everything I could find online, no AI bot could help me so here I am.

TW: bad code

Here is the script for my frontend vue app:

<script>
import { io } from 'socket.io-client';
import JoinView from './views/JoinView.vue';
import LobbyView from './views/LobbyView.vue';
import ChatView from './views/ChatView.vue';

export default {
  data() {
    return {
      socket: null,
      //other variables here
    };
  },
  mounted() {
    this.socket = io('https://**redacted**.onrender.com');
    this.initSocketListeners();
  },
  components: {
    JoinView,
    LobbyView,
    ChatView
  },
  methods: {
    // My methods are here
    },
  },
};
</script>

Backend Node.js:

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const https = require('https');
var cors = require('cors') 
const io = require('socket.io')(https, {
  handlePreflightRequest: (req, res) => {
    console.log("Entered Preflight Request")
    const headers = {
      "Access-Control-Allow-Headers": "Content-Type, Authorization",
      "Access-Control-Allow-Origin": req.headers.origin, 
      "Access-Control-Allow-Credentials": true
    };
    res.writeHead(200, headers);
    res.end();
  }
});

let rooms = [];
let users = [];

io.on('connection', (socket) => {
  console.log('User connected');

//my socket.io methods here.
}); 
const server = https.createServer(app);

server.listen(PORT, () => {
  console.log(`server listening on port ${PORT} (HTTPS)`);
});

This is the state of the code at this point, but there have been several renditions, as I have tried anything I could find on the web. AI has also pointed me in a bunch of directions but nothing.

I’ve tried setting cors stuff like this:

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', 'https://**subdomain.mydomain.ca'); 
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
  next();
});

Just recently, I tried adding the port of which the Render deployment is listening on, which is 10000. It does get rid of the cors error, but just makes the server not respond at all…

**redacted**.onrender.com:10000/socket.io/?EIO=4&transport=polling&t=OysE3Z9:1 
        
        
       Failed to load resource: net::ERR_CONNECTION_TIMED_OUT
index-CL58avdU.js:21 
        
        
       GET https://**redacted**.onrender.com:10000/socket.io/?EIO=4&transport=polling&t=OysE8k3 net::ERR_CONNECTION_TIMED_OUT

New contributor

Austin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật