不知道如何开始在JetBrains WebStorm中调试Node.js代码(Do not know how to start debugging a Node.js code in JetBrain

编程入门 行业动态 更新时间:2024-10-26 00:21:54
不知道如何开始在JetBrains WebStorm中调试Node.js代码(Do not know how to start debugging a Node.js code in JetBrains WebStorm)

我正在尝试学习在WebStorm中调试Node.js代码。 代码是这里简单的“入门”代码:

const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });

在命令行运行node app.js并访问http:// localhost:3000后,上面的代码正常工作。 接下来,我尝试在JetBrains WebStorm中按照此处的说明在本地调试它。 这是WebStorm中Node.js应用程序的调试配置:

我点击bug图标在调试模式下运行Node.js代码。 这是输出:

"C:\Program Files (x86)\JetBrains\WebStorm 10.0.3\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" --debug-brk=24163 --nolazy --inspect app.js Debugger listening on ws://127.0.0.1:24163/bffa51bd-74bd-4257-8174-2c8ab3768e17 For help see https://nodejs.org/en/docs/inspector

我想这相当于运行“ node app.js ”命令。 该指令说我需要“执行将触发代码与断点执行的步骤。” 我猜这意味着我在浏览器中打开URL“ http:// localhost:3000 ”,向app.js正在实施的Web服务器发送请求,我做到了。 我在源代码app.js的每一行都设置了断点,并且我预计断点应该在源代码中的某处,但浏览器报告“无法连接”错误,并且WebStorm中没有任何事情发生 - 没有任何断点。 我检查了指示,但无法弄清楚我缺少的东西。 你能帮我解决这个问题吗? 如果您需要更多信息,请告诉我。 非常感谢!

PS:app.js位于... \ test文件夹中。

==编辑:==

PS2:浏览器可以打印“Hello world”只是因为“node app.js”仍在运行。 我终止后,浏览器报告“无法连接”。 这意味着URL不知道如何连接到由WebStorm调试器启动的Web服务器。 端口错了吗?

PS3:这是从http:// localhost:31496 / json / list返回的内容 (端口号在每个调试会话中都会更改)。 希望它可以帮助解决问题。

description "node.js instance" devtoolsFrontendUrl "chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:31496/0f03cac0-648f-4c13-9cb9-39bb5e3a81a6" faviconUrl "https://nodejs.org/static/favicon.ico" id "0f03cac0-648f-4c13-9cb9-39bb5e3a81a6" title "app.js" type "node" url "file://E:..._test_app.js" webSocketDebuggerUrl "ws://127.0.0.1:31496/0f03cac0-648f-4c13-9cb9-39bb5e3a81a6"

I am trying to learn to debug a Node.js code in WebStorm. The code is the simple "Getting Started" code from here:

const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });

The above code works correctly after I ran node app.js at command line and visited http://localhost:3000. Next I tried to debug it locally in JetBrains WebStorm following instructions here. This is the debug configuration of Node.js application in WebStorm:

I clicked the bug icon to run the Node.js code under debugging mode. This is the output:

"C:\Program Files (x86)\JetBrains\WebStorm 10.0.3\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" --debug-brk=24163 --nolazy --inspect app.js Debugger listening on ws://127.0.0.1:24163/bffa51bd-74bd-4257-8174-2c8ab3768e17 For help see https://nodejs.org/en/docs/inspector

I guess that it is equivalent to running the "node app.js" command. The instruction says I need to "Perform the steps that will trigger the execution of the code with the breakpoints." I guess that means I open the URL "http://localhost:3000" in browser to send a request to the web server that the app.js is implementing, and I did it. I have set up breakpoints on every line of the source app.js and I expected that the breakpoints should be hit somewhere in the source, but the browser reported an "Unable to connect" error and nothing happened in WebStorm -- no breakpoint is hit. I have checked the instructions but could not figure out what I am missing. Could you please help me with this problem? If you need more information please let me know. Thanks a lot!

PS: app.js is in the ...\test folder.

==Edit:==

PS2: The browser can print "Hello world" just because "node app.js" is still running. After I terminated it, the browser reports "Unable to connect". So that means the URL does not know how to connect to the webserver launched by WebStorm debugger. Is the port wrong?

PS3: This is what returns from http://localhost:31496/json/list (the port number changes in every debugging session). Hope it can help troubleshooting.

description "node.js instance" devtoolsFrontendUrl "chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:31496/0f03cac0-648f-4c13-9cb9-39bb5e3a81a6" faviconUrl "https://nodejs.org/static/favicon.ico" id "0f03cac0-648f-4c13-9cb9-39bb5e3a81a6" title "app.js" type "node" url "file://E:..._test_app.js" webSocketDebuggerUrl "ws://127.0.0.1:31496/0f03cac0-648f-4c13-9cb9-39bb5e3a81a6"

最满意答案

据我所知,您正在使用webStorm 10.它已经很老了,并且不支持最近Node.js更新中引入的新调试器协议。

请将Node.js降级到v.4.x(至少),或者更好的是将Webstorm升级到2017.3

As far as I can see, you are using webStorm 10. It's quite old and doesn't support new debugger protocol introduced in recent Node.js updates.

Please downgrade Node.js to v. 4.x (at least) or, better, upgrade Webstorm to 2017.3

更多推荐

本文发布于:2023-08-01 01:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351712.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   Node   JetBrains   WebStorm   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!