How to test localhost node APIs on Android device
Running localhost on android physical device

Hello everyone. In this post we will look into running localhost APIs developed using Node js and Express on Android physical device. When you develop APIs using express then you need to listen to particular port on server and when working on local machine server you do the same - something like this:
const app = require('express')
app.listen(3000,()=>{
console.log('listening at port 3000');
});Here we are listening at port 3000. When you run this file using node <filename> , your local server starts and you should be able to access localhost:3000 on your browser without any error.
Problem:
When you try to access these APIs and same url localhost:3000 over your USB connected android device you will not be able to connect and get failed to connect error.
Solution:
Simple - you need to forward the port and url to your device. How? Let’s see
- First check USB debugging is enabled for your device and authorised for debugging.
- Go to
chrome://inspectin your laptop browser and you should see list of connected devices.

As you can see for me Oneplus device is shown in list.
3. Click on Port forwarding and fill in the details like below to enable port forwarding.

Note: If you are using any other port number use your port number and also don’t forget to check Enable port forwarding.
4. Click Done
5. Go to chrome browser of your device and run localhost:3000 you should be able to see your node js app running.
Application:
You can use localhost:3000 inside your android app as well and test it against this URL.
Thanks for reading. If you find the post helpful, cheer me with claps. Spread and help others learn.

