Search code examples
node.jsnetwork-programmingwifiip-address

ERR_CONNECTION_REFUSED error when i try to access the server running on localhost on laptop from my mobile devise connected to same wifi network


I want to access the server that I have build using node.js from my android phone. My server is running on localhost port 80 and my laptop and mobile device is connected to the same wifi network.
I tried turning off the windows defender firewall but it didn't work. In my mobile device browser, I used the link to make a request to my server as

laptop_ip_address:port_no/home

But it didn't work.
I get the ERR_CONNECTION_REFUSED error in my browser.
I don't want to use ngrok. what should I do?
this is my server code
const express=require("express")

const app=express()
app.get("/",(req,res)=>{
    res.status(200).send("<h1>hiiii</h1><a href='/home'>hii</a>")
    
}) 
app.get("/home",(req,res)=>{
    res.status(200).send("<h1>yooo</h1>")
}) 

app.listen(80,()=>{
    console.log("App is running");
})

Solution

  • Try after adding cors module. This may help to solve the issue.

    const cors=require('cors')
    const app=express()
    
    app.use(cors);
    
    app.get("/",(req,res)=>{
        res.status(200).send("<h1>hiiii</h1><a href='/home'>hii</a>")
        
    }) 
    app.get("/home",(req,res)=>{
        res.status(200).send("<h1>yooo</h1>")
    }) 
    
    app.listen(80,()=>{
        console.log("App is running");
    })