How to disconnect from localhost? - node.js

Is it possible to disconnect from localhost?
I'm writing a Node.js WebSocket server, and I want to test locally what would happen if the connection closes erroneously.
If I were testing remotely, I'd just Turn Wi-Fi Off, but that doesn't disconnect my connection to localhost.
Thoughts?

localhost is just an alias in your hosts file. If you remove that alias then you'll be effectively "disconnecting" from localhost.

I don't know of any way you would do what your asking except perhaps to block the ports or the program you are running on your localhost via its firewall.

As David mentioned, you can block ports with a simple firewall.
For example on OSX, to block localhost on port 8080
$ sudo ipfw add deny tcp from any to localhost 8080
Will return a response like:
00100 deny tcp from any to 127.0.0.1 dst-port 8080
And then to remove the rule:
sudo ipfw delete 00100
(ipfw is deprecated in favor of pfctl, but I still find it simpler for these purposes)

Instead of using localhost, I use the IPv4 address (which can be obtained using ipconfig call or looking into Local Area Connection) to access local machine, and then, to simulate network failure, I just disable Local Area Connection. It helps me test network failures on local machine.

Related

How to change port's Local Address

I want to open port: 8332 on my Linux Ubuntu 18.04 dedicated server.
I already opened it with sudo ufw allow 8332 command and also on server comand panel, but when I want to check if it is open it's still closed.
Checked with sudo netstat -ltup and saw that port is using local address localhost instead of 0.0.0.0
So it is accesible only from localhost and not any address right?
Is there a way to change port's Local Address to 0.0.0.0?
It should be, but this needs to be configured in the application that is listening on port 8332.

Node.js Express setup proxy for development environment, avoid localhost in url for Mac OS X

I want to setup my dev environment so that all requests from www.dev.com in my browser are routed to localhost:8080.
I added 127.0.01 www.dev.com in my /etc/host, but it doesn't do the port forwarding. If I go to http://www.dev.com:8080/ it works, but I want/need to use http://www.dev.com/ instead, which is closer to the actual production environment.
How do I do that? I tried several solutions like nginx, but I don't like it, I'd prefer it to be "scriptable", so that any other developer can use it directly. I'm using Express.js with Node.
I read through Assigning a domain name to localhost for development environment on Mac OS X with node.js but it actually doesn't explain the port-forwarding part.
You can use Mac port forwarding. Binding to port 80 requires root privileges (anything below 1024(?) needs root) and it's probably best not to run a development application with root.
You can use pfctl port forwarding
e.g. To forward port 80 to 8080
echo "rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port 8080" | sudo pfctl -ef -
source: https://salferrarello.com/mac-pfctl-port-forwarding/
You can also use ipfw (Not available on El Capitan)
sudo ipfw add 100 fwd 127.0.0.1,8080 tcp from any to any 80 in

ECONNREFUSED when using node with nano and couchdb

I was using nodejs + nano + couchdb for my application successfully up until today. For some reason all of a sudden I'm getting ECONNREFUSED when I try to run my application. If I try to query the database using the web browser or using a different application (java application) it works fine. I'm uncertain why just in this scenario it stopped working. I've been researching for the past 2 days and can't find any help. I believe this might have something to do with too many open connections, but that's a little bit out of my realm of knowledge. Can anyone provide me with any insight on debugging this issue or any direction I could go in? I should mention this couchdb lives on iriscouch
Add more information about stack that you're using. But basically it's server machine doesn't want to allow connecting. Also try run your app with DEBUG=*, nano will log via console.log almost everything.
E.g. change in package.json start command to node changetoyourapp.js DEBUG=*
I faced yesterday same issue with nodejitsu/iriscouch. Issue disappeared after some restarts.
Check the version of your node vs the expected node version of nano. It is possible that nano does not work with node > 16.
This is down to Node v18 now preferring an IPv6 address over and IPv4 address if two exist for the same hostname.
i.e. if your /etc/hosts contains entries like this:
127.0.0.1 localhost
::1 localhost
Node v16 will say that "localhost" resolves to 127.0.0.1 where Node v18 will say "localhost" resolves to ::1, the IPv6 equivalent. As CouchDB doesn't listen on an IPv6 port by default, then a connection to ::1 will be refused.
Solutions:
Use 127.0.0.1 instead of localhost in your URLs.
Use a domain name that resolves unambiguously to an IPv4 address e.g. 127.0.0.1 my.pretend.host in your /etc/hosts file.
Revert to Nodev16 which preferred IPv4 addresses in its dns lookup.
Make CouchDB bind to an IPv6 address by changing bind_address = ::1 in couchdb.ini. You can then do curl 'http://USER:PASS#[::1]:5984/.
See
https://github.com/apache/couchdb-nano/issues/313#issuecomment-1321760360

How can I find where node.js is running?

I have a VPS with node.js installed, I already uploaded a basic example to test it on the server, so I tried doing this:
I access by SSH, navigate to my project folder and run
node app.js
I get this message
Express server listening on port 8080
I thought i could see my app here
example.com:8080 or server.example:8080... but nothing. Then I tried with the info from os.networkInterfaces(); and os.host(); and still nothing happen
could you help me out? as you can see I am a total noob on node.js. What I'm doing wrong? or what should I do before running my app? Something related to DNS's? i have no idea
How do you ssh to your host? with ip or name? Is it something like:
ssh root#example.com
if so then at least you know your DNS is ok.
Once on the server do a
netstat -a
if you find *:8080 then your server is listening in the default ip. If you see something like 12.23.45.67:8080 then this number is the ip your server is listening.
ifconfig
will give you the servers ip. This should be the same as the ip of example.com. If not then maybe there is some router/firewall in front of your server and you have to configure that to allow port 8080 to reach your server.
if someone ever has the same problem this is how i solved on CentOS:
Open this file
/ Etc / csf / csf.conf
Add the required port
Allow incoming TCP ports
TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995,26"
Allow outgoing TCP ports
TCP_OUT = "20,21,22,25,37,43,53,80,110,113,443,587,873"
Restart
# # Csf-r

How to disable loopback interface in Linux (Fedora)?

So that requests to localhost are treated as if coming from remote host in LAN?
Have you tried updating your /etc/hosts file, replacing:
127.0.0.1 localhost
With your actual IP address? e.g.,
192.168.0.42 localhost
(This doesn't actually disable the loopback address, you can still connect to 127.0.0.1, but connecting to localhost should come in through your network interface, which I believe is what you're asking)

Resources