How to test localhost node APIs on Android device

Neeraj Moudgil
Sep 5, 2018 · 2 min read

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

  1. First check USB debugging is enabled for your device and authorised for debugging.
  2. Go to chrome://inspect in 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.

Neeraj Moudgil

Written by

Full Stack Android developer | Cricket | Learn Share Spread

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade