sanket kheni
sanket kheni

Reputation: 1628

How to receive http request on my local machine from the remote device on the internet?

I am developing an app to learn serverside. I have created a node js server and an android app.

WorkFlow⚙️(What I want to achieve):- WorkFlow

My local IP of pc: 192.168.0.120

On the port I am listening:8443

The whole thing working fine in localhost: as I am sending POST req. on 192.168.0.120:8443 on clicking the button on my app.

But this will only work if I am connected to my wifi not when connected to the SIM network or somewhere remote location.

So my question is where to send a request by clicking the button in my app (definitely can't send on 192.168.0.120:8443 as I am won't be connected to wifi)?

server.js file

    let http = require('http')
    
    let server = http.createServer(function (req, res) {
        if (req.method === 'POST') {
            console.log('sleeping..💤💤')
            var ffi = require('ffi-napi')
            var powrprof = ffi.Library('powrprof.dll', {
                SetSuspendState: ['int', ['int', 'int', 'int']],
            })
    
            function invokeStandby() {
                powrprof.SetSuspendState(0, 0, 0)
            }
            // invokeStandby()
        } else {
            res.writeHead(405, { 'Content-Type': 'text/plain' })
            res.end('Method Not Allowed\n')
        }
    })
    
    server.listen(8443)

    console.log('Server running on port 8443')

Upvotes: 2

Views: 2084

Answers (2)

kevintechie
kevintechie

Reputation: 1521

This is more of a networking question than a node question. You'll have to be able to configure your gateway router / firewall to make it work. In addition, your ISP must permit inbound connections on the ports you're listening to. Fortunately, this likely isn't going to be an issue, but just something to be aware of.

First, you'll need to configure your router to do port forwarding. Port forwarding will translate connections to a specific port on your router and then forward that request to the same port on a specific internal IP address on your local network. If your router has a firewall, you may also have to create a rule to let traffic on that port through. Most home routers won't need to do this.

Once your gateway router is set up, you'll need to find out the external IP address of your router. To find the external IP address you can go to a website such as https://whatismyipaddress.com/. Give this IP address along with the port to whoever you want to connect to your server.

Most ISPs assign IP addresses dynamically, so you'll have to check to see if your IP address has changed from time to time.

Once this is all set up and ports are forwarded to your local dev machine, you can launch your Node server and start seeing requests.

Be aware there are some risks with exposing your machine to the internet. Just be sure that you don't trust input to your server and maybe turn off port forwarding when you don't need it.

If you're not able to do any router configuration, look into ngrok. This will get though almost any NAT router or firewall. Be aware that the free version is limited to 40 connections per minute.

Upvotes: 3

sanket kheni
sanket kheni

Reputation: 1628

For achieving this:-

  1. I created a react-native app with a simple button. enter image description here
  2. Created node js server for putting pc to sleep when we receive any post requests. (this just works on the local network)
  3. I used ngrok to create a tunnel. (now if I POST a request on that URL my pc goes to sleep)
  4. And installed that server.ts file as windows service in pc. so whenever my pc starts of wakes up from the sleep server.ts start an HTTP server and generate a new tunnel URL. then saves it to firestore database.
  5. when I open my app in the phone it gets the tunnel URL from the firestore and when pressing the button it sends a POST request on that URL and PC goes to sleep💤💤

If you want to build it for yourself here is the URL:- Github repo

Upvotes: 0

Related Questions