我刚开始使用 Node.js,但我有使用客户端 JavaScript 和 PHP/Apache 的经验。
在您的浏览器中,您可以随时从开发人员控制台执行 JS 代码。您可以操作全局变量并调用全局函数和方法。如果我从浏览器的开发人员控制台调用 console.log('Hello world!')
,它会将消息记录到我正在使用的控制台,就像在您编写的脚本中一样。
在 Node.js 中,当您从“应用程序”中调用 console.log('Hello world!')
时,结果会按照您的预期进行记录,就像客户端上的 JS -边。我可以做我想在客户端做的事吗?
我的问题是我能不能像在客户端一样从命令行执行我自己的JS代码,操作变量(例如HTTP服务器、文件系统等)和调用函数在我的应用程序中可用?我不在乎为什么,我只是想知道我是否能做到。
V8 comes with an extensive debugger which is accessible out-of-process
via a simple TCP protocol. Node has a built-in client for this
debugger. To use this, start Node with the debug argument; a prompt
will appear:
Node's debugger client doesn't support the full range of commands, but
simple step and inspection is possible. By putting the statement
debugger; into the source code of your script, you will enable a
breakpoint.
http://nodejs.org/api/debugger.html
是的,这是可能的。您只需要在您希望代码停止并使用命令行的任何地方编写语句 debugger
(并使用 debug 参数启动 Node ,例如:node debug app.js