1

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: enter image description here

user3667111
  • 611
  • 6
  • 21
  • Why don't you examine the request sent by the before and after code? My guess is that one is setting `no-cors` and one isn't? – Kevin Workman Dec 31 '18 at 18:37
  • How can I explicity set no-cors in p5js? @KevinWorkman – user3667111 Dec 31 '18 at 19:42
  • I've added the network in an edit @KevinWorkman – user3667111 Dec 31 '18 at 20:10
  • Have you tried [express' CORS middleware](https://expressjs.com/en/resources/middleware/cors.html) ? – George Profenza Dec 31 '18 at 23:52
  • I just tried it @GeorgeProfenza still get the same error – user3667111 Jan 01 '19 at 11:46
  • Since the request is going to a third-party site, Express' CORS settings seem unrelated to the problem. Try making the request with https://github.com/Rob--W/cors-anywhere and see if that works. You don't want to make the request wth `no-cors` because you can't use an opaque response (no JSON payload). – ggorlen Jul 22 '22 at 15:03

0 Answers0