nodeJS踩坑-记本地地址127.0.0.1与0.0.0.0的区别

本文解释了在Node.js中创建Web服务时如何正确配置监听地址。重点对比了127.0.0.1与0.0.0.0的区别,并说明了0.0.0.0在监听所有IPv4地址时的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注:本文和Node.js本身关系不大,只是这个这个知识点由学习Node时候引出,主要是关于网络的一个小知识点

问题的提出

nodejs中创建web服务时,作为小白的我自然是跟着官网文档走起

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

然后很自然本地就可以用localhost或者127.0.0.1访问了。那我想用本机ip访问呢?那就再listen(192.168.1.10) listen(localhost)啥的吧?结果却不行。我再挂到服务器上想要通过ip访问?怎么也访问不成功。
开始觉得是不是端口问题,设置了半天云主机的防火墙啥的还是没有用。果然太年轻,一开始就把问题方向搞错了。

问题的基本解决

看看官网的文档有这么一句:

server.listen(port, [hostname], [backlog], [callback])#
Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).

意思就是说如果不填写主机名hostname字段,默认是监听INADDR_ANY,也就是任意的IPv4地址。
所以解决这个问题就很简单了直接listen(1337)或者listen(1337,'0.0.0.0')就可以了。
这个0.0.0.0也就是所谓的INADDR_ANY
问题是解决了可是这个知识点还要进一步理清。

127.0.0.1与0.0.0.0

127.0.0.1是一个回送地址,指本地机,一般用来测试使用。大家常用来ping 127.0.0.1来看本地ip/tcp正不正常,如能ping通即可正常使用。对于大多数习惯用localhost的来说,实质上就是指向127.0.0.1这个本地IP地址。在操作系统中有个配置文件将localhost与127.0.0.1绑定在了一起。可以理解为本地主机的意思。
这个知识点应该是没有什么问题的。其实不只有127.0.0.1,127.x.x.x都是本机回送地址(Loopback Address),只不过在系统配置中把localhost和127.0.0.1做了绑定,我们习惯用它而已。那么这个INADDR_ANY又是什么呢?

(<a href="http://www.dztcsd.com/">资质代办</a>)

INADDR_ANY

这个也叫通配地址,也就是说它不单单指本机。
0.0.0.0/8可以表示本网络中的所有主机
0.0.0.0/32可以用作本机的源地址
0.0.0.0/8也可表示本网络上的某个特定主机
综合起来可以说0.0.0.0表示整个网络
忽然想起来上计算机网络课程的时候有个默认路由的设置:

在路由器配置中可用0.0.0.0/0表示默认路由,作用是帮助路由器发送路由表中无法查询的包。如果设置了全零网络的路由,路由表中无法查询的包都将送到全零网络的路由中去。

结论

在我们刚刚nodejs配置web服务的语境下。0.0.0.0可以表示所有可以访问的地址,也就是本机的所有IPv4地址。

基础知识还是要扎实掌握才好,不然不会为了这么一点东西而大费周章。也让我想起了在腾讯面试中被问起的TCP四次挥手过程,改日写博客详细记录。希望通过博客的点点滴滴助力我更扎实的成长。

关注 http://blog.kbiao.me/ 看一个预备役码农的成长手记!


作者: K_Biao 
  
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作!
(d2l_env) root@wang-VMware-Virtual-Platform:/home/wang# jupyter notebook [I 2025-08-18 15:31:49.084 ServerApp] jupyter_lsp | extension was successfully linked. [I 2025-08-18 15:31:49.087 ServerApp] jupyter_server_terminals | extension was successfully linked. [I 2025-08-18 15:31:49.091 ServerApp] jupyterlab | extension was successfully linked. [I 2025-08-18 15:31:49.095 ServerApp] notebook | extension was successfully linked. [I 2025-08-18 15:31:49.334 ServerApp] notebook_shim | extension was successfully linked. [I 2025-08-18 15:31:49.347 ServerApp] notebook_shim | extension was successfully loaded. [I 2025-08-18 15:31:49.348 ServerApp] jupyter_lsp | extension was successfully loaded. [I 2025-08-18 15:31:49.349 ServerApp] jupyter_server_terminals | extension was successfully loaded. [I 2025-08-18 15:31:49.351 LabApp] JupyterLab extension loaded from /root/miniconda3/envs/d2l_env/lib/python3.11/site-packages/jupyterlab [I 2025-08-18 15:31:49.351 LabApp] JupyterLab application directory is /root/miniconda3/envs/d2l_env/share/jupyter/lab [I 2025-08-18 15:31:49.352 LabApp] Extension Manager is 'pypi'. [I 2025-08-18 15:31:49.391 ServerApp] jupyterlab | extension was successfully loaded. [I 2025-08-18 15:31:49.394 ServerApp] notebook | extension was successfully loaded. [I 2025-08-18 15:31:49.394 ServerApp] The port 8888 is already in use, trying another port. [I 2025-08-18 15:31:49.395 ServerApp] The port 8889 is already in use, trying another port. [C 2025-08-18 15:31:49.395 ServerApp] Running as root is not recommended. Use --allow-root to bypass. (d2l_env) root@wang-VMware-Virtual-Platform:/home/wang# jupyter notebook --ip=0.0.0.0 --port=8888 [I 2025-08-18 15:32:09.743 ServerApp] jupyter_lsp | extension was successfully linked. [I 2025-08-18 15:32:09.747 ServerApp] jupyter_server_terminals | extension was successfully linked. [I 2025-08-18 15:32:09.750 ServerApp] jupyterlab | extension was successfully linked. [I 2025-08-18 15:32:09.753 ServerApp] notebook | extension was successfully linked. [I 2025-08-18 15:32:09.993 ServerApp] notebook_shim | extension was successfully linked. [I 2025-08-18 15:32:10.006 ServerApp] notebook_shim | extension was successfully loaded. [I 2025-08-18 15:32:10.008 ServerApp] jupyter_lsp | extension was successfully loaded. [I 2025-08-18 15:32:10.009 ServerApp] jupyter_server_terminals | extension was successfully loaded. [I 2025-08-18 15:32:10.011 LabApp] JupyterLab extension loaded from /root/miniconda3/envs/d2l_env/lib/python3.11/site-packages/jupyterlab [I 2025-08-18 15:32:10.011 LabApp] JupyterLab application directory is /root/miniconda3/envs/d2l_env/share/jupyter/lab [I 2025-08-18 15:32:10.011 LabApp] Extension Manager is 'pypi'. [I 2025-08-18 15:32:10.049 ServerApp] jupyterlab | extension was successfully loaded. [I 2025-08-18 15:32:10.052 ServerApp] notebook | extension was successfully loaded. [I 2025-08-18 15:32:10.053 ServerApp] The port 8888 is already in use, trying another port. [I 2025-08-18 15:32:10.053 ServerApp] The port 8889 is already in use, trying another port. [C 2025-08-18 15:32:10.053 ServerApp] Running as root is not recommended. Use --allow-root to bypass. (d2l_env) root@wang-VMware-Virtual-Platform:/home/wang# jupyter notebook --allow-root [I 2025-08-18 15:32:33.381 ServerApp] jupyter_lsp | extension was successfully linked. [I 2025-08-18 15:32:33.384 ServerApp] jupyter_server_terminals | extension was successfully linked. [I 2025-08-18 15:32:33.389 ServerApp] jupyterlab | extension was successfully linked. [I 2025-08-18 15:32:33.392 ServerApp] notebook | extension was successfully linked. [I 2025-08-18 15:32:33.630 ServerApp] notebook_shim | extension was successfully linked. [I 2025-08-18 15:32:33.643 ServerApp] notebook_shim | extension was successfully loaded. [I 2025-08-18 15:32:33.644 ServerApp] jupyter_lsp | extension was successfully loaded. [I 2025-08-18 15:32:33.645 ServerApp] jupyter_server_terminals | extension was successfully loaded. [I 2025-08-18 15:32:33.647 LabApp] JupyterLab extension loaded from /root/miniconda3/envs/d2l_env/lib/python3.11/site-packages/jupyterlab [I 2025-08-18 15:32:33.647 LabApp] JupyterLab application directory is /root/miniconda3/envs/d2l_env/share/jupyter/lab [I 2025-08-18 15:32:33.647 LabApp] Extension Manager is 'pypi'. [I 2025-08-18 15:32:33.685 ServerApp] jupyterlab | extension was successfully loaded. [I 2025-08-18 15:32:33.688 ServerApp] notebook | extension was successfully loaded. [I 2025-08-18 15:32:33.688 ServerApp] The port 8888 is already in use, trying another port. [I 2025-08-18 15:32:33.688 ServerApp] The port 8889 is already in use, trying another port. [I 2025-08-18 15:32:33.689 ServerApp] Serving notebooks from local directory: /home/wang [I 2025-08-18 15:32:33.689 ServerApp] Jupyter Server 2.16.0 is running at: [I 2025-08-18 15:32:33.689 ServerApp] http://localhost:8890/tree?token=1e0460c42c80c5618f39a5208bc667b8765429bd0a202b55 [I 2025-08-18 15:32:33.689 ServerApp] http://127.0.0.1:8890/tree?token=1e0460c42c80c5618f39a5208bc667b8765429bd0a202b55 [I 2025-08-18 15:32:33.689 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 2025-08-18 15:32:33.810 ServerApp] To access the server, open this file in a browser: file:///root/.local/share/jupyter/runtime/jpserver-10883-open.html Or copy and paste one of these URLs: http://localhost:8890/tree?token=1e0460c42c80c5618f39a5208bc667b8765429bd0a202b55 http://127.0.0.1:8890/tree?token=1e0460c42c80c5618f39a5208bc667b8765429bd0a202b55 [I 2025-08-18 15:32:33.823 ServerApp] Skipped non-installed server(s): bash-language-server, dockerfile-language-server-nodejs, javascript-typescript-langserver, jedi-language-server, julia-language-server, pyright, python-language-server, python-lsp-server, r-languageserver, sql-language-server, texlab, typescript-language-server, unified-language-server, vscode-css-languageserver-bin, vscode-html-languageserver-bin, vscode-json-languageserver-bin, yaml-language-server Opening "/root/.local/share/jupyter/runtime/jpserver-10883-open.html" with Text Editor (text/html) (gnome-text-editor:11112): Gtk-WARNING **: 15:32:34.255: Unable to acquire session bus: 执行子进程“dbus-launch”失败(没有那个文件或目录) mkdir: cannot create directory ‘/run/user/0’: Permission denied Authorization required, but no authorization protocol specified Error: cannot open display: :0 mkdir: cannot create directory ‘/run/user/0’: Permission denied Authorization required, but no authorization protocol specified Error: cannot open display: :0 /usr/bin/xdg-open: 882: iceweasel: not found /usr/bin/xdg-open: 882: seamonkey: not found /usr/bin/xdg-open: 882: mozilla: not found /usr/bin/xdg-open: 882: epiphany: not found /usr/bin/xdg-open: 882: konqueror: not found /usr/bin/xdg-open: 882: chromium: not found /usr/bin/xdg-open: 882: chromium-browser: not found /usr/bin/xdg-open: 882: google-chrome: not found /usr/bin/xdg-open: 882: www-browser: not found /usr/bin/xdg-open: 882: links2: not found /usr/bin/xdg-open: 882: elinks: not found /usr/bin/xdg-open: 882: links: not found /usr/bin/xdg-open: 882: lynx: not found /usr/bin/xdg-open: 882: w3m: not found xdg-open: no method available for opening 'file:///root/.local/share/jupyter/runtime/jpserver-10883-open.html' context mismatch in svga_surface_destroy
最新发布
08-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值