为什么 serverReadyAction 配置在浏览器中打开 127.0.0.1 而不是 localhost?

huangapple go评论99阅读模式
英文:

Why serverReadyAction configuration open the 127.0.0.1 rather than localhost in browser?

问题

我已经按照在调试服务器程序时自动打开URI文档创建了一个调试配置来调试我的程序。我已经添加了serverReadyAction功能以在我的浏览器中打开URL。uriFormat已设置为"http://localhost:%s"。然而,VS Code在我的浏览器中打开的是http://127.0.0.1:3000而不是http://localhost:3000。我该如何修复这个问题?

我正在使用WSL(Ubuntu 22.04)。

这是我的.vscode/launch.json配置:

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "ts-node",
  6. "type": "node",
  7. "request": "launch",
  8. "args": [
  9. "${file}"
  10. ],
  11. "runtimeArgs": [
  12. "-r",
  13. "ts-node/register"
  14. ],
  15. "cwd": "${workspaceRoot}",
  16. "console": "integratedTerminal",
  17. "serverReadyAction": {
  18. "pattern": "listening on.* (https?://\\S+|[0-9]+)",
  19. "uriFormat": "http://localhost:%s",
  20. "action": "openExternally"
  21. }
  22. }
  23. ]
  24. }

以及我的app.js文件:

  1. const express = require('express');
  2. const app = express();
  3. app.get('/', (req, res) => {
  4. res.send('Hello World!');
  5. });
  6. app.listen(3000, () => console.log('Example app listening on port 3000!'));
英文:

I have followed the Automatically open a URI when debugging a server program document to create a configuration to debug my program. I have added the serverReadyAction feature to open the URL in my browser. The uriFormat is set to "http://localhost:%s". However, VS Code opens http://127.0.0.1:3000 instead of http://localhost:3000 in my browser. How can I fix this?

I am using WSL (Ubuntu 22.04).

Here is my .vscode/launch.json configuration:

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "ts-node",
  6. "type": "node",
  7. "request": "launch",
  8. "args": [
  9. "${file}"
  10. ],
  11. "runtimeArgs": [
  12. "-r",
  13. "ts-node/register"
  14. ],
  15. "cwd": "${workspaceRoot}",
  16. "console": "integratedTerminal",
  17. "serverReadyAction": {
  18. "pattern": "listening on.* (https?://\\S+|[0-9]+)",
  19. "uriFormat": "http://localhost:%s",
  20. "action": "openExternally"
  21. }
  22. }
  23. ]
  24. }

And my app.js file:

  1. const express = require('express');
  2. const app = express();
  3. app.get('/', (req, res) => {
  4. res.send('Hello World!');
  5. });
  6. app.listen(3000, () => console.log('Example app listening on port 3000!'));

答案1

得分: 0

打开 Windows 的 hosts 文件

  1. C:\Windows\System32\drivers\etc\hosts

取消注释符号 #127.0.0.1 localhost DNS 记录。

hosts 文件:

  1. # localhost 名称解析由 DNS 自身处理。
  2. 127.0.0.1 localhost
  3. # ::1 localhost

如果 VS Code 已经连接到 WSL,请从命令面板中选择 Remote: Close Remote Connection。然后重新连接到 WSL。

再次调试程序,然后 VS Code 将在浏览器中打开 http://localhost:3000

英文:

Open the hosts file of windows

  1. C:\Windows\System32\drivers\etc\hosts

Remove the comment symbol # for the 127.0.0.1 localhost DNS record.

hosts:

  1. # localhost name resolution is handled within DNS itself.
  2. 127.0.0.1 localhost
  3. # ::1 localhost

If VS Code has already connected to WSL, choose Remote: Close Remote Connection from the command palette. Then reconnect to WSL.

Debug program again, then VS Code will open http://localhost:3000 in the browser.

huangapple
  • 本文由 发表于 2023年6月16日 10:22:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定