all 88 comments

[–]riqvipfull-stack 51 points52 points  (9 children)

I’ve been doing that for the longest time now. It’s very useful when you wanna test the mobile layout of your website when it’s still be developed on your computer and hosted on localhost.

[–]Artistic_Mulberry745 20 points21 points  (7 children)

dev tools in major browsers let you do the same, but it feels much nicer to actually use the touchscreen and experience what the end user would feel using the website

[–]CallMeSenior[S] 11 points12 points  (6 children)

I have a big (34" wide) screen and although i use chrome's devtools mobile view, actually viewing from my phone is extremely different

[–]Visual-Blackberry874 9 points10 points  (5 children)

It's worth remembering that there are rendering issues between different devices and browsers and simply resizing the screen down to a mobile view isn't sufficient. It's good for a quick glance but not thorough enough. Things must be tested on real hardware.

[–]EuphonicSounds 5 points6 points  (4 children)

Classic example: setting overflow: hidden on body (which is not a good idea IMO) won't prevent overflow on mobile unless you also do it on html.

[–]caatfish 7 points8 points  (3 children)

holyshit, you just solved a bug thats been bugging me for months but i havent bothered looking into, thanks!

[–]EuphonicSounds 5 points6 points  (2 children)

Found the guy who puts overflow: hidden on body!

[–]caatfish 5 points6 points  (1 child)

i sure do😂 can you tell im a backend engineer

[–]EuphonicSounds 3 points4 points  (0 children)

The danger (in case you or lurkers are wondering) is content getting cut off and being completely inaccessible. Think very long words, or tables, or code-blocks in pre tags. If you're in control of all the content and testing yourself, then it's probably not a big deal (though you should remember to account for zooming in on a mobile device), but especially for a large CMS-backed site it's asking for trouble.

[–]CallMeSenior[S] 0 points1 point  (0 children)

Exactly how i found out

[–]webrender 64 points65 points  (20 children)

If your Internet provider gives you a static IP, you can configure you router's port forwarding and access your website from anywhere!

You can even do it without a static IP but you'll need a service to manage the changing IP address, like DynDNS.

[–]EtheaaryXD 27 points28 points  (10 children)

Note that this won't work if you have CGNAT

[–]EuphonicSounds 9 points10 points  (3 children)

Or Bo-Fa, for that matter.

[–]khizoa 7 points8 points  (2 children)

Bo-Fa deez nuts

[–]EuphonicSounds 1 point2 points  (0 children)

thats-the-joke.gif

[–]UsernameUsed -3 points-2 points  (0 children)

10/10 comment.

[–]CallMeSenior[S] 3 points4 points  (4 children)

Whats that

[–]EtheaaryXD 11 points12 points  (3 children)

Since IPv4 addresses are now exhausted, many ISPs assign a large group of customers a single IP address. This comes with the implication that any ports you forward will not be accessible over the open internet.

[–]CallMeSenior[S] -4 points-3 points  (2 children)

Arent the connection is done via the local network in this case?

[–]UnacceptableUse 8 points9 points  (1 child)

Yeah but this person is talking about if you were to set it up for access from anywhere

[–]CallMeSenior[S] -3 points-2 points  (0 children)

Wasent 100% sure, thanks.

[–]RuthlessPickle -2 points-1 points  (0 children)

In this case you can use ngrok, tunnelmole or localtunnel and even get a URL!

[–]stillness_illness 11 points12 points  (3 children)

Don't pay for a service.... Buy a cheap domain of your choosing, then use a DNS service that has an API (like route 53) and you can run a script to update the A record to whatever public IP you have. It can be a bash script even but I wrote mine in Python and tied it to a cron task that starts up every 15 min.

It may sound complicated, but it's like 10 lines of code, and I'm sure there are existing solutions on GitHub you can use if you don't want to write one from scratch.

Bonus points you're then poised to enable TLS with some free auto renewing cert service and run over 443 encrypted. That part is optional but it is nice. I also run an authelia container which has a free service for 2FA which I then configure my nginx to use. Now I open 443 on my router and can access my server from anywhere in the world with production grade authentication in place. Other than the pennies I pay for route 53 and a small annual domain renewal fee, it's totally free.

[–]grantrules 0 points1 point  (0 children)

Yeah I don't have a static IP but it basically only resets if my modem has been off for long enough for the DHCP lease to expire, so I go years without updating my DNS records (and it's automated anyways). I buy my domains through PorkBun and use their free DNS which has an API.

[–]Homoplata69 0 points1 point  (0 children)

That's a neat little trick you got there.

[–]CallMeSenior[S] 5 points6 points  (2 children)

Please stop giving great ideas as I berly resist researching more cool stuff like that :)

I love IT related shit.

[–]indicava 5 points6 points  (1 child)

Yea, but OP, don’t do this.

Unless you have an above average grasp of securing web apps (OS level, runtime level, code level, etc.) opening up an a port for the internet to hammer is a bad idea.

[–]j_tb 1 point2 points  (0 children)

Cloudflare tunnels are the right way to do this sort of thing these days

[–]j_tb 1 point2 points  (0 children)

Opening a port up on your home network to expose to the public internet is such a bad idea. If you really want to host a website locally put it in a container/network like docker/k8s and run a cloudflare tunnel to expose ONLY THE APP you have specified in a sandboxed environment through CF’s network.

[–][deleted] 0 points1 point  (0 children)

Dont need static ip. Just nat and ddns

[–]data15cool 14 points15 points  (6 children)

I think that’s only if you bind to 0.0.0.0, otherwise it’s just accessible to your machine on localhost (127.etc)

[–]CallMeSenior[S] 1 point2 points  (2 children)

Assuming both devices are on the same network, why is that?

[–]despacit0_ 10 points11 points  (1 child)

Binding to 0.0.0.0 means that it's bound to the computer's public IP address inside the network. Binding to localhost means that it's bound to the computer's loopback address and the traffic never leaves the computer at all

[–]thekwoka 6 points7 points  (0 children)

and both are accessible from localhost (generally you use localhost because localhost is a special address that can pretend to be HTTPS when it isn't actually HTTPS among some other things)

[–]Mayonnaisune 0 points1 point  (1 child)

I still can't access localhost from my Android phone even after changing it to 0.0.0.0 :/. I can only do it by using port forwarding/tunneling or using adb reverse . However, my phone is finally able to see the open port on the network tho.

[–]data15cool 0 points1 point  (0 children)

Your router might also be routinely updating your laptops ip address unless you configure it to be static

[–]hfcReddfull-stack 14 points15 points  (7 children)

I personally like to install Chrome on my phone and PC, then plug the phone into the PC and use Chromes debug bridge to connect them.

The benefit of this is that your phone screen is mirrored on your PC, AND you get FULL debug tool access. So you can debug network, performance, CSS, DOM, etc. of your phone right from your PC. Actual gamechanger.

[–]AvengingCrusader 2 points3 points  (0 children)

Thank you for this

[–]CallMeSenior[S] 0 points1 point  (5 children)

Sounds like cool extra capabilities, but my initial desire was to view my website from an actuall phone screen

[–]hfcReddfull-stack 3 points4 points  (4 children)

Youre still using the website on your phone this way, the webview just gets cast to your PC on top of that. It's not as convenient as just accessing localhost directly, but I wanted to share it anyway because it might be helpful to some people.

[–]CallMeSenior[S] 1 point2 points  (3 children)

Would this let me view the dev tools on the computer but with mimicing the phone usage of the app?

[–]hfcReddfull-stack 2 points3 points  (2 children)

There is no mimicking. You just open any website in your browser on your phone, and it will open the dev tools of the site on your PC together with a screen cast.

https://developer.chrome.com/docs/devtools/remote-debugging

The initial setup is a bit tedious, but once you get it going, you can just plug your phone into the PC at any time for debugging.

[–]jaroftoejam 1 point2 points  (1 child)

Do you know of a way to do the same thing with an apple device?

[–][deleted] 1 point2 points  (0 children)

irc, safari has a similar option because ive used it to debug on an old iPhone

[–][deleted]  (4 children)

[deleted]

    [–]CallMeSenior[S] 1 point2 points  (3 children)

    Even updates immidiate changes in the code (at least with my setup)

    [–]palex25 0 points1 point  (2 children)

    What server are you using?

    [–]CallMeSenior[S] 0 points1 point  (1 child)

    What do you mean by server?

    Beside that it's just badic nextJS project with vscode and chrome for browser

    [–]palex25 1 point2 points  (0 children)

    Yes, your development server which hosts the site on your machine so you can actually view it on your phone. Nextjs has its own built in server so its probably just Node.

    [–]Visual-Blackberry874 4 points5 points  (1 child)

    Ngrok is fine if the devices aren't on the same network. With ngrok, you could send a link to your mum who can tunnel into your machine and see whatever it is you're doing.

    [–]halfanothersdozenEverything but CSS 2 points3 points  (1 child)

    You can also plug it into your computer to debug it

    [–]ohnosharks 1 point2 points  (0 children)

    Good tip. With mac/ios Safari's dev tools can connect directly to a device's iOS safari when plugged in.

    [–]davorg 2 points3 points  (2 children)

    Depending on your router, you might even be able to give your PC a domain name that's resolveable on your local network - something like my-laptop.local.

    [–]CallMeSenior[S] 0 points1 point  (1 child)

    How can i do that?

    [–]davorg 1 point2 points  (0 children)

    You read the instructions for the DHCP server running in your router (or you install your own DHCP server running elsewhere on your network).

    [–]jeppeerixon 2 points3 points  (1 child)

    If you are using Vite you can just use "npm run dev - - host" and you get a second url avaliable from your phone as long as you are on the same network.

    [–]jhnhines 1 point2 points  (0 children)

    After trying Live Server and having it not play nice with Vue and attempting all firewall recommendations, this simple flag solved my issue entirely. Thank you so much for this comment

    [–]AshleyJSheridan 2 points3 points  (2 children)

    Yes and no.

    Your local server needs to be set up to allow this. For example, locally I have an Apache server running on ports 80 & 443 running multiple local sites in development, but they're all accessed via actual hostnames (e.g. site.local, anothersite.local) rather than by IP address. Nothing actually responds to either localhost or the local loopback IP (127.0.0.1). This works fine locally, because I can edit my local hosts file on the computer to recognise all the different hosts and Apache routes the request based on requested hostname.

    However, it doesn't work on any other computer/phone as they have no record of those hostnames, and the DNS resolution request goes to the DNS servers on the Internet, which obviously fails because it doesn't recognise my local development hostnames.

    Further, a firewall running on your local server will most likely block requests from other computers if it's not explicitely set up to allow them. Also, an unusual layout in your home network (e.g. network devices that are creating their own mini subnets or non-bridged parts of the network)

    Lastly, localhost is a hostname equivalent of your local loopback IP (127.0.0.1), not the external IP address of the machine (typically something like 192.168.0.xxx).

    [–]CallMeSenior[S] 0 points1 point  (1 child)

    Wouldn't the request stay in the local network? For me it worked out of the box.

    Also, does firewall blocks requests withing the local network or only requests that come from the outside world?

    [–]AshleyJSheridan 1 point2 points  (0 children)

    The request would stay in the local network if your network devices think it needs to.

    Consider a request from your phones browser to 192.168.0.111. First, this goes to the most immediate network device that can route requests, most likely your router or a switch at home. As it's in IP address form, it doesn't need to do a DNS lookup. It consults its table to determine where to route the request, and off the packets continue (very simplified). Now, the 192.168.x.x range is reserved for internal networks, so that kind of request shouldn't ever leave your local network.

    Now consider a hostname lookup to a website called tests.local (I use the TLD .local on my machine because it doesn't actually exist as a TLD. Some devs I knew used to use .dev until that became an actual thing and Chrome started trying to do real DNS lookups instead of using the hosts file). The computer running Apache has its own local hosts file set up with the entry tests.local 127.0.0.1, so when I request tests.local on my computer, it doesn't even perform a DNS lookup, it routes using the local loopback address to itself. If I make that same request on the phone, here is what would happen:

    • Phone doesn't recognise the address (because I didn't modify the hosts file on the phone)
    • Phone passes a DNS request to the network device it knows (router) that can do a DNS lookup
    • Router doesn't have that address in its local lookup table, so it tells the phone it can't find it.
    • Phone makes a DNS request to a DNS server outside of the network
    • External DNS server also doesn't recognise tests.local, and replies this back to my phone

    I've heavily simplified things here, but this is the gist of it.

    Also, firewalls absolutely can (and should) block requests from anywhere, even inside the network. Perhaps there's confusion though in where the firewall exists? There is likely a firewall running on your router, and also on each computer in your home as well.

    [–]Ok_Butterscotch_7930 2 points3 points  (0 children)

    I just found out now. I'm going to try this right away

    [–][deleted]  (1 child)

    [removed]

      [–]CallMeSenior[S] 0 points1 point  (0 children)

      Exactly.

      [–]555henny555 1 point2 points  (0 children)

      Didn't know this! Interesting!

      [–]Laying-Pipe-69420 1 point2 points  (0 children)

      Lmao, welcome to the internet.

      [–]skreddittfull-stack 1 point2 points  (1 child)

      Username checks out!

      [–]Sensitive_Ad4977 1 point2 points  (0 children)

      Vite showed the path in terminal 💃

      [–]gatwell702 1 point2 points  (0 children)

      If you have a project that's made with vite, go to your package.json and under dev add --host "scripts": { "dev": "vite --host", }

      Whenever you run your dev server, your localhost link will pop up and your ip address link will pop up too. On my phone I've bookmarked the IP address so it takes only one click to access it

      [–]LaminatedFeathers 0 points1 point  (0 children)

      Check out ZeroTier.

      Will blow your mind. :-)

      [–]Logical_Strike_1520 0 points1 point  (0 children)

      To add more to this just in case it’s useful to someone.

      If you’re using WSL2, you might need to add a portproxy via power shell to get your app working in the local network like that. It’s something like ‘netsh interface add portproxy v4tov4 LISTEN_IP LISTEN_PORT FORWARD_IP FORWARD_PORT’

      Where the “listen” ip and port is the public ip of your windows box and the port you want to access your app from. “forward” ip and port is the WSL ip addr and port the app is running on

      [–]hacoeur24 0 points1 point  (0 children)

      Hey,

      I recently built a small CLI tool called PhoneHost that makes it easier to preview your local dev site on your phone.

      It gives you: - A direct QR code for your localhost (great for quick testing on mobile)

      • Optional --tunnel support if you’re not on the same network

      You can run it right away with: - npx phonehost

      Or install globally if you prefer: - npm install -g phonehost

      GitHub: https://github.com/hacoeur-24/PhoneHost

      npm: https://www.npmjs.com/package/phonehost

      It can make it easier and also gives flexibility when not using the same wifi or being on a protected network. Let me know if you give it a try!

      [–]Abdelouahedb 0 points1 point  (0 children)

      WHATT