Can hit Cloud9 service with curl but not with fetch



Here’s what happens in a bash tab:

rhedin:~/workspace $ curl -X GET
<!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express</p></body></html>rhedin:~/workspace $ 
rhedin:~/workspace $ 

And here’s what happens in a REPL tab:

  .then(response => response.json())
  .then(data => console.log('data is', data))
  .catch(error => console.log('error is', error));
error isTypeError {stack: "TypeError: Failed to fetch", message: "Failed to fetch"}

I don’t get it. Why don’t those two behave the same? Something in the environment?


This code in the server solved the problem.

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

I think it also helped that I installed the whatwg-fetch polyfill. I think the error message “Hey, Buddy, you have a CORS problem” showed up in the console after I installed the polyfill.

Delete this or leave it if you think it will be helpful to others, as you will.