Joe
Joe

Reputation: 13151

Angular - can not serve on EC2

I created Angular app on EC2 (RHEL 7) with ng new app1 and tried to serve it with:

ng new app1
cd app1
ng serve --port=4200

When I go to url: IP:4200 - getting following error:

Exception:  tcp_error
Exception details:  A communication error occurred: "Connection refused"

But when I try with NodeJS on the same server and same port - I can see the result on the same IP:4200 . NodeJS code is:

var express = require('express');
var app = express();
app.get('/', function (req, res) {
  res.send('Hello World 3!');
});
app.listen(4200, function () {
  console.log('Example app listening on port 4500!');
});

What could be wrong with Angular and EC2? I can run the exact same Angular code on my local computer and when go to localhost:4200 - I can see results (but not from EC2 IP:4200).

Upvotes: 0

Views: 1191

Answers (2)

WENJUN CHEN
WENJUN CHEN

Reputation: 11

  1. use "netstat -na | grep :4200" to check whether 4200 port is open
  2. use telnet 127.0.0.1 4200 to check whether you can connect to this port via internal ip
  3. use telnet [external ip] 4200 to check whether you can connect this port via external ip

To locate the issue.

Upvotes: 1

DWilches
DWilches

Reputation: 23065

According to the output of netstat, Angular's server is configured to listen only for connections that target the ip 127.0.0.1.

But connections from outside will target a different IP, so you need to pass that other IP with the --host param. Or, to make it easier, use the IP 0.0.0.0 which means "every IP that this host responds to".

So the command should be like:

ng serve --port=4200 --host=0.0.0.0

Upvotes: 4

Related Questions