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

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

Related

Connecting to host from inside a docker container on linux requires opening firewall port

Background: I'm trying to have XDebug connect to my IDE from within a docker container (my php app is running inside a container on my development machine). On my Macbook, it has no issue doing this. However, on linux, I discovered that from within the container, the port I was using (9000) was not visibile on the host gateway (Using sudo nmap -sT -p- 172.20.0.1 where 172.20.0.1 is my host gateway in docker).
I was able to fix this issue by opening port 9000 on my development machine (sudo ufw allow 9000/tcp). Once I did this, the container could see port 9000 on the host gateway.
My Question: Is this completely necessary? I don't love the idea of opening up a firewall port just so a docker container, running on my machine, can connect to it. Is there a more secure alternative to this?
From what you've told us, opening the port does sound necessary. If a firewall blocks a port, all traffic over that port is blocked and you won't be able to use the application on the container from the host machine.
What you can do to make this more secure is to specify a specific interface to open the port for as specified here:
ufw allow in on docker0 port 9000 proto tcp
Obviously replace docker0 with the docker interface on your machine. You can find this by looking at the output of ip address show or by following the steps here if the interface name is not obvious.

How do I make a NodeJs project publicly accessible on port 3000?

I have a NodeJs/Express project in Alibaba cloud based Ubuntu server.
When I run project and access with curl localhost:3000 and curl 127.0.0.1:3000 it works!
When I access with IP public, e.g. curl 192.x.x.x:3000 it doesn't work, even though I have edited config in Express project in some code to : server.listen(3000,"0.0.0.0") OR server.listen("3000","192.x.x.x").
FYI I have Apache on this server. When I access on Internet with IP public no problem.
What can I do to solve this problem? Thanks beforehand.
PS: the 192.x.x.x is my IP public and it works access with Apache project
Issue the following command to open port 3000 for TCP traffic.
sudo ufw allow 3000/tcp
You have to configure your security ground and create a inbound rule to allow port 3000. Follow this guideline.
https://www.alibabacloud.com/help/doc-detail/25471.htm
Make sure you allow TCP traffic or all traffic from all sources to the port 3000 as the inbound rule.
The fact that you can access your service locally - but not publicly could mean 2 possible configurations:
The server running your application has blocked the port 3000
You have not configured your server to map the port 80 of a specific route to the port 3000
It is highly possible that a most essential part of your server configuration has not been done.

nodejs timed out on all ports when hosting on godaddy server

I've trying to run my nodejs/expressjs application on my godaddy server, but any port I use times out. I've tried using the application on my local device and it works fine. I have a snippet of my connection below.
var app = express();
app.listen(8080, function() {
console.log("Listening on port " + 8080);
});
When I run the program through ssh, I get no errors
node index.js
Listening on port 8080
But when I go to the corresponding location in my browser, I get:
xxx took too long to respond.
ERR_CONNECTION_TIMED_OUT
I'm pretty sure it has to do with running on the godaddy server. If anyone has experience using this service with nodejs, is there a specific port I should be using, or is there any other setup I should do?
Do you have a VPS with GoDaddy right? So I assume you have also root access.
SSH into your GoDaddy server as root and check if the node.js app actually listens on that port:
netstat -tunlp | grep 8080
If you see any result there for the node.js app and that port then the port is open.
By default, there should be a firewall on your server which might block most of the ports and allows only the necessary incoming traffic.
You can check if there is any rule for that port by issuing the command bellow:
iptables -nvL | grep 8080
If any result is returned, then you have to add an iptables rule to allow access to that port. There are multiple methods to do that:
permit full access from your IP access to the server
permit your ip to access port 8080 on the godaddy server
permit outside world to access port 8080 on your server
You could read any iptables guy, it's pretty easy to add/edit/delete firewall rules. Most of the cPanel/WHM servers come with CSF Firewall (which is based on iptables and perl scripts).
In order to allow an ip address to your firewall (if you have CSF Firewall installed) you have to issue the following command:
csf -a ip-address
I hope that helps!

amazon ec2 service(linux) cannot use tomcat7 or 6

i just use yum install tomcat7 to setup the tomcat7 and change the port, in the /usr/share/tomcat/conf/server.xml from 8080 to 80 and service tomcat6 start, it works fine.
but when i do the netstat -nlp, there is no 80 port, and also other cannot visit the 80 port
try to create ROOT, i think you did not create it yet, that way make your website unavaialbel.
When you do netstat it typically will show you http, not 80: this is because 80 is bound to http in /etc/services. You'll see something like this:
tcp 0 0 *:http *:* LISTEN
Assuming you're not experiencing a Tomcat error, make sure that you've set up the EC2 security group to allow access to port 80. Look at this for a decent treatment.
EDIT: if 8080 works but 80 doesn't then it is either:
Some other program (such as Apache) sitting on port 80.
You're probably not running with the right privileges. On most Linux distributions you need to be the root user (or running as a system process) to access ports numbered less than 1023

How to disconnect from localhost?

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.

Resources