CORS/Express: Getting data from server from domain html


Joined
Jul 18, 2020
Messages
1
Reaction score
0
Howdy!

This has been many days of trying to research however, either I am misunderstanding something or the solution online is not directred toward my issue.

I am running a node server on a sub-domain as such: xxx.yyy.com

I have express, socket.io and cors all installed.

My app.js file:

JavaScript:
const http = require('http');
const express = require('express');
const app = express();
const cors = require('cors');
const server = require('http').createServer(app);

const path = require('path');

const io = require("socket.io")(server);

app.use(cors());

app.use('/', express.static(path.join(__dirname, 'public')));

io.on("connection", (socket) => {
 
});

server.listen();

In my public folder I have a file 'testCors.html' which just outputs, Hello World.

Outside of the server, I have a file located here: www.yyy.com/testCors.php


JavaScript:
<script src="_js/jquery-3.6.0.min.js"></script>

<script>
$( document ).ready(function() {

$.ajax({
type: "GET",
url: 'https://xxx.yyy.com/testCors.html',
dataType: "html",
success: function(result) {
alert(result);
}
});

});
</script>

As you can see, I just want the yyy.com/testCors.php page to alert 'Hello World' as found from xxx.yyy.com/textCors.html

However, I continue to get the CORS error.

Any related issues I can find online on stackoverflow, YT and here and other forums seems to talk about one port on localhost to another port, or receiving API from external domain, however I want my php page on public_html to receive.

I have also tried setting headers with PHP on client side but not having any luck with that either.

Any guidance will be greatly appreciated!
 
Ad

Advertisements

Joined
Mar 11, 2022
Messages
167
Reaction score
22
First of all u should use https instead http. This isn't help your with cors (we get to that in a sec), but without SSL nowadays cors is your slighest smallest problem.

CORS:
You have'nt set any rule for CORS e.g:
Code:
cors: {
    origin: "*",
    methods: ["GET", "POST"],
    transports: ['websocket', 'polling'],
    credentials: true
  },allowEIO3: true,
  handlePreflightRequest: (req, res) => {
        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();
    }
//out of my server.js replace * with your domain.

But that also may not solve your problem. Socket.io is the best but also the hardest to get to work. Your server settings also need to match as your subdomain settings must too. There's a lot of work to be done before chatting or other magic (e.g. video chat) is going on.
 
Ad

Advertisements

Joined
Mar 11, 2022
Messages
167
Reaction score
22
Ah, by the way. server.listen() ???????
listen to what? listen expects an argument -> PORT. (So an 4 digits number)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top