Can't connect to Node web-server hosted on Amazon EC2 - node.js

I'm running a node app on an Amazon EC2. The app includes a simple web server intended to serve the index page, but it doesn't work.
Here's the server code:
var http = require('http'),
fs = require('fs'),
io = require('socket.io'),
index;
fs.readFile('client.html', function(err, data){
if (err){
throw err;
}
index = data;
});
var server = http.createServer(function(request, response){
response.writeHeader(200,{"Content-Type": "text/html"});
response.write(index);
response.end();
}).listen(1223);
The EC2 is assigned the public IP address 54.187.31.42. I run the app, open my browser, connect to 54.187.31.42:1223 expecting to be served a web page and get nothing.
What is wrong with my server code?
I used the snippet found in this answer to check the IP of the EC2 running the app, and oddly get 172.31.3.67 - why is the returned address different from the one assigned to the machine by Amazon?
Consequently, trying to connect to 172.31.3.67:1223 also fails.
Straight from the Amazon dev controls, if that helps confirm it isn't an issue of the server IP being wrong or something.

The code looks fine, try connecting with the public IP/public DNS that you see in the AWS console.
Try the following and your application would work:
Open the port (in your case 1223) in the security groups of your instance.
stop the firewall on your machine (i.e. iptables) and now access your server using public ip or public DNS.
If you can now acceess your machine that means something in the iptables is filtering your traffic. You can modify the iptables rules accordingly.

In security group, add a rule with the type "Custom TCP Rule" on the used port (e.g. port: 3000 or 1223 for this case). It works for me.

Related

How to connect to node.js server from any ip

I want to create a private backend for an application I want to make, but I am having trouble connecting to my node server, I have the basic stuff right now,
var http = require("http");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<html><body><h1>Hello World</h1></body></html>');
}).listen(3000);
console.log('Server running on port 300.')
But this only works for https://localhost:3000/, how do I make it so that if I have a separate computer on a separate connection, I can connect to this server?
I am not interested in just opening it to everyone but just to specific client IP's...
If the client IP's are on the same network as you, you can check out this question
If you want people from anywhere to access your application, I suggest hosting it on something like Heroku (very easy to deploy, pretty good free tier). You can then create a whitelist of IPs in your express application.
I would suggest for any port forwarding using ngrok or configuration in your router
For downloading ngrok https://ngrok.com/ go to this link
For configuration your router it will take some searching in google based on what type of router your using
You must mention that your localhost or Nat Ip and your public IP to resolve here is NOIP refrence https://www.noip.com/support/knowledgebase/general-port-forwarding-guide/
As you specified that you want the backend to be private, such that it can only be accessed by your specified node. You will have to host this node server on a cloud service or you can host it on your local machine by opening a port for the node server. Suppose you host the node server on port 1234 on your local machine ip address.
You can start the node server on localhost and your desired port, but you need to allow requests to the particular port.
Now you need to check for the origin of the request that your node server receives and validate that, so that only your private node(computer) can access the node server. You can do that by validating the hostname using express, you can get the hostname of the request in express.js using req.hostname or req.headers.host.
You would need to use express.js for this functionality and the code would be as follows
let express = require('express');
let app = express();
let allowedHost = 134.32.234.3 // the hostname which is allowed to access the backend
let port = 1234; // desired port
let host = 0.0.0.0 // desired host; 0.0.0.0 to host on your ip
app.get((req, res) => {
res.header('Content-Type', 'text/html');
if(req.hostname == allowedHost){
res.send('<html><body><h1>Hello World</h1></body></html>');
}
else{
res.send('Connection not allowed');
}
});
app.listen(host, port, ()=>{
console.log(`The server is running on http://${host}:${port}`);
}

dns server configuration for node.js

I have developed server application in node.js. Right now I access the application using 128.1.1.5:3000. But i want to use a domain name like abc.net to access the application. How can I do this. Please suggest.
To configure DNS on your local app,you need to do following configuration.
Make an entry of this DNS example abc.net as a host instead of local host while setting up your node server where you are mentioning the localhost host and port detail eg. in app.js file.
Example
const http = require('http');
const hostname = 'abc.net';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Now open command prompt and type
ipconfig -all
It will list all your IPs. Select the ip of your machine which is preferred one.mostly you can locate this ip by finding the ip which is followed by (preferred) keyword in command prompt.
Now copy this IP address and make an entry of this in system host file.Make sure you have an admin rights to make changes in this file.
Path of host file in Windows
C:\Windows\System32\drivers\etc\hosts
Edit this file and scroll to the end and press Enter to copy the ip address corresponding to the DNS which you have configured in node js application as shown below in new line.
IPaddress(fetched in step 3)
abc.net
i.e ipaddress then give space then dns name
Save the file.
Start your node application.
Now try hitting your api from the url abc.net:port/api
You will need a domain that you can edit the DNS settings on and add an A record that is configured to your server's external IP address
then you can access your domain with the port attached
example: mydomain.com:5000
you should refer to your domain record provider's documentation on how to do this.
Beyond that, you may encounter firewall settings, port settings, and possible HTTPS certificates issues but these are separate topics each.

Formatting node apps for AWS deployment

I'm trying to deploy a node.js app on aws EC2 Beanstalk. My problem is, I can't figure out how to move from my localhost testing environment to aws standard. Right now, my app works on port 8081 by using the following code.
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
})
How would I change this server variable to work on an actual domain?
Assuming your intent is to provide a public-facing web application, your code will work as is, albeit with a few caveats:
Currently your server will listen on port 8081. Once deployed to AWS users would have to browse to www.somedomain.com:8081 to reach your application. (Assuming the host instance allows traffic on that port. See below).
If your intent is to have users reach your application at www.somedomain.com - without specifying a port - you'll want the server to listen on port 80 instead.
var server = app.listen(80, function () { ... }
In either case you'll need to ensure that the security group rules for the EC2 host instance allow incoming TCP traffic on the listening port. Likewise, if your EC2 host instance is behind a load balancer you'll need to allow incoming traffic on the appropriate ports there as well.
For something a little fancier, you can try deploying your application to Elastic Beanstalk using Docker and exposing port 8081 in the dockerfile. This way users would still reach it at www.somedomain.com (via http port 80) and you could continue to develop and test locally using port 8081.
One final note: you didn't provide much information about what your application is or how you intend to use it, so I'm making quite a few assumptions based only on the information provided.
This code works great for me with node on Elastic Beanstalk, and allows me to seamlessly switch between localhost and remote development without changing any code:
var port = process.env.PORT || 8081;
var server = app.listen(port, function () {
//server is started!!!
})

Running peerjs-server on OpenShift returns 503 Service Unavailable

I have set up up a node.js 0.10 gear in OpenShift which I deployed a simple server which is based off peerjs-server. All I want this server to do is act as a signalling server to communicate the connection info between peers connected to my application and from then on they communicate peer-to-peer using WebRTC. Everything works when pointing to the demo "PeerJS Cloud" signalling server but when trying to use my own server set up I keep getting returned 503 status codes.
Here is the server creation code I use:
var host = process.env.OPENSHIFT_NODEJS_IP;
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server = new PeerServer({ port: port, host: host});
NB: I have added host to peerjs-server so I can use OpenShift's IP, not sure if this was necessary but it wasn't working without this either.
The server peerjs-server uses is restify. Here is the server create and listen code:
this._app = restify.createServer(this._options.ssl);
/* A lot of set up code that I have not changed from peerjs-server */
this._app.listen(this._options.port, this._options.host);
Where this._options.port and this._options.host are the ones defined in the previous code segment and I am not using SSL so nothing is being passed in there.
When deploying this code to OpenShift I get no errors but when accessing the site on port 80 or 8000 (the open external ports) I get 503's. I also checked rhc tail and this is what I get:
Screenshot (Can't post images because I have no reputation..). Not sure exactly what that means if anything.
Any help is much appreciated, and if more info is needed I can add more, was not sure what was important information or not.
UPDATE: It's a scaled application using 1-3 small gears.
from https://github.com/peers/peerjs-server/blob/master/lib/server.js:
// Listen on user-specified port and IP address.
if (this._options.ip) {
this._app.listen(this._options.port, this._options.ip);
} else {
this._app.listen(this._options.port);
}
So, use 'ip' and not 'host'. Worked for me.

node.js install on Amazon EC2

So I'm installing node.js on amazon ec2 with ubuntu 8.04, and and have run node sayhello.js which is this code:
var sys = require('sys'),
http = require('http');
http.createServer(function (req, res) {
setTimeout(function () {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<br/><strong> Hello World!</strong>');
res.end();
sys.puts(sys.inspect(req, false));
}, 2000);
}).listen(8000);
sys.puts('Server running at http://ec2-174-12-132-193.compute-1.amazonaws.com:8000/');
I see
Server running at
http://ec2-174-12-132-193.compute-1.amazonaws.com:8000/
being displayed in the console correctly.
The tutorial says: go to :8000 in the browser and you should see Hello World!
I go to http://ec2-174-12-132-193.compute-1.amazonaws.com:8000/ (not the real address) but it doesn't load (just connecting...). The example uses localhost, is doing the public domain incorrect or some such?
Thanks.
You need to open up port 8000 in your security group.
If you've got the EC2 command line tools installed, try running:
$ ec2-authorize default -p 8000
This assumes that you're using the default security group. If not, change default to the name of your security group.
If you're just using the web interface follow these steps:
Login to the AWS console
Select Amazon EC2 in the top bar
Click on Security Groups in the menu on the left
Click on the security group that you assigned to your EC2 instance (probably just default)
In the bottom window pane, click on the Inbound tab
Set Port range: to 8000 and leave the other two inputs as they are
Click Add Rule
Within the security group associated with the EC2 instance, make sure you have port 8000 open to your IP or to the public.
Check the following:
That you are allowing access from all ips to port 8000 in the security group
That you have modified the CORRECT security group (e.g., quicklaunch-1) and not, say, quicklaunch-2. I've edited the wrong one more times than I care to admit.
That you have opened port 8000 on the Linux firewall
That your server is actually running (you should see "Listening on port 8000" in the command line).
Here is a tutorial on how to set up a Node.js web server on Amazon EC2: http://www.lauradhamilton.com/how-to-set-up-a-nodejs-web-server-on-amazon-ec2
It's a bit more complicated than what you're doing (because it uses ip forwarding from 80 to 8080) but the part about opening the Linux firewall is the same.

Resources