【问题标题】:ERR_CONNECTION_REFUSED error when i try to access the server running on localhost on laptop from my mobile devise connected to same wifi networkERR_CONNECTION_REFUSED 错误,当我尝试从连接到同一 wifi 网络的移动设备访问笔记本电脑上本地主机上运行的服务器时
【发布时间】:2021-11-26 17:16:05
【问题描述】:

我想从我的安卓手机访问我使用 node.js 构建的服务器。我的服务器在 localhost 端口 80 上运行,我的笔记本电脑和移动设备连接到同一个 wifi 网络。
我尝试关闭 Windows Defender 防火墙,但没有奏效。 在我的移动设备浏览器中,我使用该链接向我的服务器发出请求

laptop_ip_address:port_no/home

但它没有用。
我在浏览器中收到 ERR_CONNECTION_REFUSED 错误。
我不想使用 ngrok。 我该怎么办?
这是我的服务器代码
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");
})

【问题讨论】:

  • Localhost为环回地址,不能在主机外的网络上使用。发送到 localhost 的任何内容都会立即在主机内部循环。您需要在连接到网络的接口上运行服务器。

标签: node.js networking wifi ip-address


【解决方案1】:

添加 cors 模块后尝试。这可能有助于解决问题。

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");
})

【讨论】:

    猜你喜欢
    • 2017-01-15
    • 1970-01-01
    • 2020-11-22
    • 2016-02-07
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 2018-12-08
    相关资源
    最近更新 更多