Can I specify a port in an entry in my /etc/hosts on OS X? [duplicate]

Solution 1:

No, the hosts file is simply a way to statically resolve names when no DNS server is present.

Solution 2:

The hosts file is for DNS resolution. DNS resolve names to IP addresses, and has nothing to do with ports I am afraid. You will need to use something else in conjunction with the hosts file to redirect the port (Mangle the TCP header by altering the destination port).

With iptables:
Does MAC OS use iptables / netfilter (I didn't think it did)..? If OS X uses iptables you could point xyz.com to some ip in the hosts file like 157.166.226.25 and then:

sudo iptables -t nat -A OUTPUT -p tcp --dport 80 -d 157.166.226.25 -j DNAT --to-destination 127.0.0.1:3000

:-)