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

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

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配置:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "ts-node",
      "type": "node",
      "request": "launch",
      "args": [
        "${file}"
      ],
      "runtimeArgs": [
        "-r",
        "ts-node/register"
      ],
      "cwd": "${workspaceRoot}",
      "console": "integratedTerminal",
      "serverReadyAction": {
        "pattern": "listening on.* (https?://\\S+|[0-9]+)",
        "uriFormat": "http://localhost:%s",
        "action": "openExternally"
      }
    }
  ]
}

以及我的app.js文件:

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

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:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "ts-node",
      "type": "node",
      "request": "launch",
      "args": [
        "${file}"
      ],
      "runtimeArgs": [
        "-r",
        "ts-node/register"
      ],
      "cwd": "${workspaceRoot}",
      "console": "integratedTerminal",
      "serverReadyAction": {
        "pattern": "listening on.* (https?://\\S+|[0-9]+)",
        "uriFormat": "http://localhost:%s",
        "action": "openExternally"
      }
    }
  ]
}

And my app.js file:

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => console.log('Example app listening on port 3000!'));

答案1

得分: 0

打开 Windows 的 hosts 文件

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

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

hosts 文件:

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

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

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

英文:

Open the hosts file of windows

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

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

hosts:

# localhost name resolution is handled within DNS itself.
	127.0.0.1       localhost
#	::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:

确定