Drakeee0909
Drakeee0909

Reputation: 67

Access NodeJs Express server from Android Phone

I have a website that runs with Node.js and express on the localhost with the port 3000, and I want to access to this localhost with my Android Phone. I've tried a few things but none of them worked for me.

Here's the index.js

const express = require('express');
const path = require('path');

const app = express();
const port = process.env.PORT || '3000';

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname + '/public/pages/index.html'))
});

app.get('/about',function(req,res){
    res.sendFile(path.join(__dirname+'/public/pages/about.html'));
});

app.use(express.static(__dirname + '/public'));

app.listen(port, () => {
  console.log(`Listening to requests on http://localhost:${port}`);
});

Upvotes: 0

Views: 6827

Answers (1)

Ojetokun
Ojetokun

Reputation: 176

Firstly your phone and laptop or desktop must be on the same network. You can share your phone's hotspot to the laptop. Then run the ipconfig /all command (on windows to check your network address). You should see IPv4 Address. . . : YOUR IP(Preferred) somewhere close to the end. That is the address you will type on your android browser.

Then you will modify your index.js file's listen function to

...
app.listen(port, "YOUR IP ADDRESS" || "localhost" ,() => {
  console.log(`Listening to requests on http://localhost:${port}`);
});

After saving that, open YOUR IP ADDRESS:PORT in your phone browser. e.g 192.53.13.8:3000 and voila.

Upvotes: 3

Related Questions