How does node-express listen both on localhost and local IP? - express

I started using node express not too long and developed a simple http server using the default settings.
I have always tested it with requests to localhost:3000 with Postman and on the browser and everything was fine.
Then I needed to move the service on the local network so that even applications not running on my machine can access it.
I got my local IP and before perform any modification I tried first with Postman using the IP address instead of localhost and also with the browser on my smartphone to check what would have happened. With my surprise it was already working on the local network, in the sense that I got a response message from the server (which might be dangerous since I did not even know).
I guess the line where you specify which address and port the server should listen on is server.listen(port);
If the address is not specified does it mean that it should listen both to localhost and local IP? Or is node-express automatically forwarding from one address to the other? Otherwise how can it work both on localhost and local IP?

node-express and all other web application servers listen to port, not IP address. That is, as long as port X is listened on your machine (server.listen(port); in node-express) and not blocked by firewall, and another host H can access your machine by IP, H can visit the web application that listen X.
If the address is not specified does it mean that it should listen both to localhost and local IP? Or is node-express automatically forwarding from one address to the other?
"port listening" happens on transport layer, while IP address belong to network layer. When listening to the HTTP request, node-express does not care about the visitor's IP address.
Is there a way to see which resource both of them are pointing to?
All clients visit resource in web application in the same way, it has nothing to do with client's IP, unless you manually configure it in the web application.

Related

Can not access web app hosted on apache from local network

As a newbie set up mediawiki in apache httpd on my linux pc. In the corresponding .conf file "require all granted'" is set. The pc was assigned static IP 192.168.0.2 by the router. The port 80 of the pc is opened for any device. I visited http://192.168.0.2/mediawiki/index.php from the host. It worked as expected. Now I tried to visit the same address from my android phone connected to the same network and I was presented with the message "127.0.0.1 refused to connect" by chrome. I noticed that the part http://192.168.0.2 had been replaced by https://127.0.0.1 in the address bar. The same thing also occurred on Firefox on my phone. I can still visit http://192.168.0.2 from my phone and it works just fine. So what is happening here and how to solve this problem?
EDIT: The problem is solved. Mediawiki has this LocalSettings.php file which gets automatically created during installation. It contains a field "$wgServer" which is the "The protocol and server name to use in fully-qualified URLs" as shown in the documentation. This value was automatically set to "http://127.0.0.1" during the installation. Thereby whenever I was trying to connect to mediawiki from my phone I was actually getting redirected to that same phone. I changed the value to "http://192.168.0.2" and now everything is working fine!
Looks like something happened at the connection from your mobile phone to the pc hosting the web server.
Your configuration looks fine even tho you provided almost no details at all: according to your description, I suspect the problem is on your phone or on your router because the domain and url switched from 192.168.0.2 to 127.0.0.1 and the protocol switched from http to https.
You should try the same test from another pc connected to the same network.
You can have a better understanding of what's happening, testing the connection from command line using curl:
$ curl -v http://192.168.0.2/mediawiki/index.php
The output will give you some details of what's going on if the redirect was requested by the webserver or not.
My guess is that your apache is configured to listen to 127.0.0.1 or localhost (it's the default). Open httpd.conf or your virtual host configuration file and look for Listen. Here's some snippets from the official documentation:
The Listen directive tells the server to accept incoming requests only on the specified port(s) or address-and-port combinations. If only a port number is specified in the Listen directive, the server listens to the given port on all interfaces. If an IP address is given as well as a port, the server will listen on the given port and interface. Multiple Listen directives may be used to specify a number of addresses and ports to listen on. The server will respond to requests from any of the listed addresses and ports.
For example, to make the server accept connections on both port 80 and port 8000, on all interfaces, use:
Listen 80
Listen 8000
To make the server accept connections on port 80 for one interface, and port 8000 on another, use
Listen 192.0.2.1:80
Listen 192.0.2.5:8000
More information at
https://httpd.apache.org/docs/2.4/bind.html

Can't see site externally

I'm brand spanking new to setting up websites, so go easy.
Basically I setup an osticket system on my XAMPP stack and It works locally no problem but when I try to access it from my public IP it won't connect.
I've port forwarded port 12345 -> 80 as my server listens on 80.
So theoretically if I type in my URL bar it should connect right?
In the httpd.conf file for Apache do I enter the external or internal IP address? I'm assuming I leave it as localhost because the .conf file is local to the server.
My networking setup:
Firewall setup:
<PublicIP:12345> ALLOW <PriavteIP>
Port forwarding setup:
<PublicIP:12345> -> <PrivateIP:80>
It's hard to tell without understanding more about your setup. I'm going to assume you have a router that connects your internal network to the Internet, and you have one or more computers connected in your network, and your server is one of those.
So are you talking about the port forwarding rules on your public-facing router's firewall? If you are forwarding port 12345 from your router to port 80 on your server, the reason it won't work is because a standard HTTP request from a public machine will come to your router on port 80, not port 12345. Are you entering in a URL with the 12345 port number in it? If anything, you should simply be routing all requests to port 80 on your router to the IP of your server.
You can check this if you try to view your web site using the port number in the URL. So if your IP address to your router is 5.6.7.8 you would enter http://5.6.7.8:12345 and this would load your web page, as the request for a page from port 12345 would get forwarded to port 80 on your server. But honestly I think you just need to get rid of port 12345.

Unable to access my website outside local network

I'm hosting my website on IIS. I am able to access the website via localhost and the system's IP address, but I'm unable to access it outside of my local network. I am able to remotely control that machine using mstsc.
I believe that the firewall is completely disabled. What could be the cause of this?
If your external IP address is different from your machines' intranet IP address, what you need is port forwarding configured in your router. I.e. external requests coming to ports 80 (HTTP) and port 443 (HTTPS) should be re-routed to your IIS PC.
Disregard port 443, if your web site is not using Secure Sockets Layer (SSL).

show my localhost in someother pc via ip

I have a site in my local which is running under xampp.Now in another Mac I want to access the local site.It can be done via sharing the ip? or something to write in host file.The source machine is windows and the target machine is mac.
I dont know how to include the ip in host file in Mac.
Assuming that it's running on port 80 or 443 and your firewall is set to allow requests on those ports, you should be able to navigate to the ip address in a browser. If you're requesting your local site from outside your subnet then you might need to configure port forwarding on your router

Apache: how can I access my webpage from a computer outside my network?

I want to access my webpage from a computer outside the network. Our network has IPs like 192.168.0.1-192.168.0.255. The network is connected to Internet through a local gateway 192.168.0.1 and gets to DNS server 193.xxx.xxx.xxx.
Let's say my computer has the IP 192.168.0.50. How can I my website from my server (Apache) from a computer which is not from our network (let's say 254.231.52.xxx)?
Thank you!
Short answer:
The solution to this would be to find out the 'external IP' of your router and enable a port forwarding for port 80 and 443 to your local IP.
Long answer:
The Internet is divided into 'public' and 'private' IP spaces. Private IPs are usually not directly accessible from a 'public' IP.
The IPs from the 192.168.0.1-255 are from one of these private subnets.
Your router (at home) usually has multiple IPs. One or more from your local private network (192.168.0.X) and one from your local ISP (I guess something like 193.xxx.xxx.xxx because your DNS is in that network) which is from the 'public' space.
To connect to your computer with the private IP from a public IP like the mentioned 254.231.52.xxx you would have to connect to the public IP of your router (the 193... one). You would also have to enable a mechanism that is called 'port forwarding'. This effectivley takes all internet traffic arriving at the public IP of your router on the forwarded ports and transfers it to the private IP you configured the forwarding to.
E.g. 254.231.52.. -> 193...:80 -> 192.168...:80
Usually home routers have a configuration page where you can do this.
The required ports you would probably use are 80 (http) and 443 (https).
Another possibility is using so called http-proxies which are also usually supported as a configuration option one home routers. You should refer to the manual of your router for configuration instructions of this.
What you need is to identify your home computer from the whole internet in order to access from outside.
This tutorial can help you
http://kyokasuigetsu25.wordpress.com/2011/01/21/how-to-access-localhost-of-computers-outside-the-network/
But if you only have plain html files ( css, js too ) you could use Dropbox and share your sites folder. This way you could be working locally and files would be synced to your public folder
Hope it helps
If you have sufficient privilege to configure the router (on the gateway machine), then you can use "port forwarding" to do that.
Go to your router settings and configure it so that it forwards the packets it receives on port 80 to your machine. Assuming your router has a valid IP itself (say x.x.x.x) you can access your Apache server on: http://x.x.x.x/.
You can use any other port instead of 80 and access your Apache through http://x.x.x.x:PORT/ of course.
If you don't have sufficient privileges, then nothing comes to my mind except using reverse tunneling using SSH or similar protocols.
Most gateways (router) have an VirtualServer option which you can point to the local server (192.168.0.50)
You could use something like dyndns.org to setup a permanent hostname to your server. i.e. mylocalserver.dyndns.org
A public name for your machine needs to be set up in DNS to point to your networks' router. Then you will need to set up your router to redirect traffic for that name to the machine in question. Also the firewall needs to be set up to allow that.