Node Js can't access to server from internet - node.js

hi first of all I have limited knowledge of network.
operating system is mac, firewall is turned off.
it works on a local network: http://192.168.1.2:8080 (add to safari)
next I got my ip address from google.com "what is my ip"
78.157.xx.xxx, so I tried from internet connect to server like:
http://78.157.xx.xxx:8080 (add to safari), but with no success :(
I have a router and 3 devices, and all these three devices have the same ip 78.157.xx.xxx?
server.js
var http = require('http');
http.createServer(function (req, res)
{
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
})
.listen(8080, "0.0.0.0");

You need an open port on your router to allow a connection in to your home network from the Internet. Then you need to create a port forward from 78.157.xx.xxx:8080 to 192.168.1.2:8080.
You can get more details about port forwarding here.
Here is a useful guide and resource to configure the router.

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}`);
}

http.createServer in nodeJS doesnt work in company network

I am a beginner programmer so my technical skills are low in general.
The following code:
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(1337, '127.0.0.1');
Runs without a problem on my home network.
But doesnt run on my Business Network.
Code in Atom:
Headers I get back after running in the browser:
If it helps, we use a Windows server 2012 for the network and I have access in it.
Is this really related to the server?
As Yerken mentioned, your URL should be http://localhost:1337, and not http://localhost/1337.
In your code, you are listening to port 1337, but by default, the browser tries to access port 80 if no port is specified. That means localhost/1337 is trying to access port 80, and not 1337 which you are listening to.
So change the URL to :1337 as oposed to /1337 and it should work :)
Happy programming!

Accessing server from ip address (Node js/Windows) [duplicate]

This question already has answers here:
Node.JS Not working on the internet
(3 answers)
Closed 9 years ago.
I have a js file with the following code
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hellosldksldksldk World\n');
}).listen(3000, '127.0.0.1');
console.log('Server running');
Now, if I access the server by 127.0.0.1:3000 its perfectly fine, but I want to access it from my own computer ip address. I write 192.xxx.x.xxx:3000, but I cant access it. Since I am developing an android application I need the ip address of the computer in order to run it, can someone explain why I am unable to access it?
When you say listen(3000, '127.0.0.1'), you're explicitly binding your server to port 3000 on IP 127.0.0.1.
You probably just want to bind to all IPs, which you can do by omitting the bind host:
listen(3000);

Hostname not working node.js

My code is as follows:
var http = require('http');
var static = require('node-static');
var file = new static.Server();
http.createServer(function (req, res) {
file.serve(req, res);
}).listen(1337, '127.0.0.1');
When the url is localhost:1337/1.html it works fine. However if I change it to hostname:1337/ where 'hostname' is the hostname of my server, I get unable to establish connection error. In PHP i could easily replace 127.0.0.1 or localhost with hostname. Why isn't the same possible in node.js?
So the problem is when your browser resolves "hostname", DNS gives it your local network IP address like 192.168.0.42, but your code tells node to listen on one and only one IP address: 127.0.0.1, so the connection doesn't work. Replace '127.0.0.1' in your node code with '0.0.0.0' (which means "all IP addresses") and things will work. Be advised that other computers on your local network (like other folks in a coffee shop wifi network) will be able to connect to your application, which is why for development sticking with the loopback IP address (127.0.0.1) and 'localhost' are better choices.

I can't access my node.js server on my AWS EC2 isntance from the outside

I'm trying to run a basic node.js server,
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('hello world!\n');
}).listen(3000, '0.0.0.0', function() {
console.log('Server running on port 3000');
});
However when I run it and go to http://x.x.x.x:3000/ the page doesn't load.
I tried the answer on this question but that didn't work either. And changing the host to 127.0.0.1 or the server ip or emitting it doesn't fix it either.
I've also followed this guide that says to proxy requests with haproxy. But that did not work either.
Is there something in the security tab I have to enable/disable?
Edit: The problem was I was using the wrong IP. The IP changes when the instance is restarted.
Create a rule to open port 3000 in the security group associated with your ec2 instance.
It can be done through the command line tools or through the web console, which is more straightforward. If you didn't specify a security group when creating the instance it will be the "default" security group.
A decent walkthrough for the console
Amazon documentation
Rightscale explanation of different firewall situations

Resources