2

I am using an AWS cloud9 IDE for my project.

I have Angular running on port 8080. I have my NodeJS (+Express) server running on port 8081.

I have CORS headers setup in my Node app.js as following:

const app = express();
app.use(express.json());
app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

I use Angular services with http client module to make requests to api endpoints (https://localhost:8081/blogposts/*).

In one of my Angular components, I make a request to fetch the data inside ngOnInit():

ngOnInit() {
    this.blogService.getBlogPosts()
        .subscribe((blogposts: BlogPost[]) => {
            this.blogposts = blogposts;
        });
}

but I get the following error in the browser console:

GET https://localhost:8081/blogposts net::ERR_CONNECTION_REFUSED

I thought this was a problem with setting up CORS headers, but as shown above I have added all required headers.

I read that a proxy can be used as an alternative solution to CORS, but this is not suitable for production environments.

I can confirm my API works fine using curl:

ubuntu:~/environment (develop) $ curl http://localhost:8081/blogposts

[{"title":"Calvin and Hobbes","authorName":"Bill Waterson", ...

I am also concerned whether the Cloud9 environment is preventing me from making these API calls, but as far as I know, both ports 8080 and 8081 are safe to use.

Can I get more help in finding what's wrong with my api?

wO_o
  • 181
  • 3
  • 13
  • Can you access `https://localhost:8081/blogposts` from Browser or Postman – Dilshan Jun 22 '20 at 12:15
  • 2
    Remove `https` and replace with `http` – Dilshan Jun 22 '20 at 12:16
  • @Dilshan yes, I've tried using curl to make a request to the API and it works. I've added the result above. – wO_o Jun 23 '20 at 21:15
  • @Dilshan and no, replacing https with http gave me the same error. But the curl command didn't work at all with https, so I think using http is indeed correct. – wO_o Jun 23 '20 at 22:34

1 Answers1

0

Cloud9 does not allow http/https connections to any port other than 8080. The team is aware of the friction this causes for developers and they're looking at way to make it better.

Richard H Boyd
  • 201
  • 1
  • 2
  • 1
    Thanks for clarifying. I realized this and have moved my workstation to Windows + WSL + docker since. – wO_o Jul 20 '20 at 19:37