💡 在开发或测试时,我们常使用 `localhost:8080` 来运行本地服务,但如何让局域网中的其他人访问它呢?别急,这里教你一招!💪
首先,确认你的电脑和目标设备在同一网络下(比如Wi-Fi)。接着,打开终端或命令行工具,输入以下命令来查看你的本机IP地址:
```bash
ifconfig macOS/Linux
ipconfig Windows
```
找到类似 `192.168.x.x` 的地址,这就是你的局域网IP!🌐
然后,回到你的项目,确保服务器绑定的是 `0.0.0.0` 而不是默认的 `localhost`。例如,在 Node.js 中可以这样设置:
```javascript
const http = require('http');
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, '0.0.0.0'); // 绑定到所有网络接口
console.log('Server running at http://0.0.0.0:8080/');
```
最后,告诉你的朋友用浏览器访问 `http://你的IP地址:8080` 即可!🎉
⚠️ 注意:开放本地服务可能带来安全风险,请仅限于可信网络中使用,并及时关闭服务!🔒