P5JS CORS error after making it a node app - javascript

So I have a P5JS app which has a very simple preload function:
function preload() {
json = loadJSON(getCurrentViewersUrl());
}
which calls this function and returns the request url:
function getCurrentViewersUrl() {
return "https://tmi.twitch.tv/group/user/" + STREAM_NAME + "/chatters";
}
This all worked swimingly when using http-server to start my P5JS app.
I've now just switched so that my P5JS code is served up by my node app.
const express = require('express');
var app = express();
var server = app.listen(9999);
app.use(express.static('public'));
But now, with making this change I'm now getting the cors policy error:
Access to fetch at
'https://tmi.twitch.tv/group/user/x/chatters' from origin 'http://localhost:9999' has been blocked by CORS policy: No
'Access-Control-Allow-Origin' header is present on the requested
resource. If an opaque response serves your needs, set the request's
mode to 'no-cors' to fetch the resource with CORS disabled.
My question is, why am I getting this now and not before?
This is what the request looks like:

Related

Handling CORS in communication through web-sockets

I'm developing a client and server app, that communicate over web-sockets and found that handling CORS only on the web-socket doesn't raise any additional CORS issues, without handling CORS on the server side itself.
Before solving the issue the error on the client side was:
Access to XMLHttpRequest at 'http://localhost:5000/socket.io/?EIO=4&transport=polling&t=NOOM0wp' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
No "Access-Control-Allow-Origin" header on the response object, that appears only if I handle CORS on the server side.
But, on the client side I get an error:
GET http://localhost:5000/socket.io/?EIO=4&transport=polling&t=NOONjI6 net::ERR_CONNECTION_REFUSED
I guess this is a pretty simple issue, still an explanation will be much appreciated.
Use cors configuration.
Refer to Handling CORS.
var express = require('express')
var app = express()
var app = express()
var server = require('http').createServer(app)
var io = require('socket.io')(server, {
cors: {
origin: "*" // add your own origin, or '*' for any origins allow.
}
})

I'm unable to figure out how to prevent getting this CORS error in Node.js [duplicate]

This question already has an answer here:
Getting a CORS error when trying to establish socket.io connection between back-end on localhost:3000 and front-end on localhost:8080
(1 answer)
Closed 2 years ago.
I'm trying to figure out how to create a real time chat application that uses Vue.js for the front-end, Node.js for the back-end and socket.io.
I've generated my back-end Node.js project with express-generator and the project is accessible at http://localhost:3000. In this project I have an app.js file that contains this:
var express = require('express');
var http = require('http').createServer(express);
var io = require('socket.io')(http);
io.on('connection', (socket) => {
console.log('a user connected');
});
My front-end is a Vue.js project accessible from http://localhost:8080 and the component that is meant to connect to my back-end contains this:
<template>
<div class="home">
</div>
</template>
<script>
import socket from 'socket.io-client'
export default {
mounted(){
socket.connect('http://localhost:3000')
}
}
</script>
Sadly the socket connection doesn't seem to go through. On the front-end console I get these errors -
localhost/:1 Access to XMLHttpRequest at 'http://localhost:3000/socket.io/?EIO=3&transport=polling&t=N6Kc6yn' from origin 'http://localhost:8080' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
And on the back-end console I get this error - GET /socket.io/?EIO=3&transport=polling&t=N6KcMan 404 3.089 ms - 975
I tried installing cors through npm install cors and added the required code but nothing really happened. It looks like this:
var express = require('express');
var cors = require('cors');
var app = express();
app.use(cors());
var http = require('http').createServer(express);
var io = require('socket.io')(http);
io.on('connection', (socket) => {
console.log('A user has connected!');
});
I can't figure out why is this happening.
Try removing this -> app.use(cors()); if the problem persist you could have a bad path.

Reactjs: No 'Access-Control-Allow-Origin' header is present on the requested resource

I added headers on the server side
res.setHeader('Access-Control-Allow-Origin','*');
res.setHeader('Access-Control-Allow-Methods',"OPIONS,GET,POST,PUT,PATCH,DELETE");
res.setHeader('Access-Control-Allow-Headers','Content-Type , Authorization');
But I still get the error on reactjs
I used fetch for send and take data.
Also I used from the header 'Access-Control-Allow-Origin':'*'
On the ractjs
you can use pretty good library called cors which can do all your configs for you. here check it cors. you can configure it in your app.js like so:
const express = require('express');
const app = express();
const cors = require('cors')
app.use(cors());
if this does not work, you probably forgot to add proxy to your client's package.json

Getting error No 'Access-Control-Allow-Origin' header is present on the requested resource

I am trying to create a headless drupal app. i am using drupal 8 as a beckend and react as a front-end. i have created REST services in drupal 8 using core rest services module. the problem is when i am calling the api its giving me error i.e
Fetch API cannot load http://192.168.1.246/headless-react/api/events. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 403. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I configured the services.yml file for cors.config but still getting error. can anyone have any idea how to solve it ? thanks
Install the CORS module https://www.drupal.org/project/cors
It will allow you to overcome the error related to cross origin
I am using extension for it. The reason why you get this warning because you called api from another domain. Or you you can use nginx.
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
You need to mention what build tools are you using in your front end which uses ReactJS.
Assuming that you are using webpack, you will need to add a proxy in the package.json located at the root directory, like so :
package.json
...
"name": "relay-starter-kit",
"proxy":"<your-base-remote-server-url-here",
"private": true,
...
For instance, if your server is at https://www.myawesomeserver.com, then
"proxy": "https://www.myawesomeserver.com".
In your case, "proxy":"http://192.168.1.246" , should work.
In your application, if you need to access the route, https://www.myawesomeserver.com/home, then you just need to pass the route and webpack will pick up the base url from proxy in package.json
A request for home will look like,
const getHome = async() => {
try{
const raw = await fetch('/home')
const res = await raw.json()
} catch(e) {
console.log(e)
}
}
This is because when you run webpack, it runs the react app on its webpack dev server and for you to communicate with any other server, webpack will have to make a request on your behalf.
If you are not using webpack, this may not be useful for you.

how to work around a Access-Control-Allow-Origin error

I have two node servers on localhost:3000 and localhost:4000.
I call 4000 from 3000 and get the following error.
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
Any suggestions for this error?
If the 4000 server is a express based node server, use "cors" package to solve it like below
var cors = require('cors');
.....
app.use(cors());
Where app is an express application.

Categories