英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论