运行Express应用程序时,是否可以通过命令行以与浏览器控制台类似的方式与其JS环境进行交互?
例如,说Express正在运行,我想通过使用普通的js将变量打印到cli来检查变量。所以我输入的内容如下:
<terminal command> console.log( variableToPrint );
答案 0 :(得分:0)
有点,您可以使用一个名为'Debugger'的程序,并且即使在较大的broser中也可以打印每个变量值,执行此命令:
node --inspect-brk index.js
然后转到网址:chrome://inspect
中的镀铬装置。如果您只想使用不带broser的命令行,请使用此文件和下一个命令运行调试器:
// myscript.js
global.x = 5;
setTimeout(() => {
debugger;
console.log('world');
}, 1000);
console.log('hello');
$ node inspect myscript.js
< Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
< For help see https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in myscript.js:1
> 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
2 setTimeout(() => {
3 debugger;
debug> cont
< hello
break in myscript.js:3
1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
2 setTimeout(() => {
> 3 debugger;
4 console.log('world');
5 }, 1000);
debug> next
break in myscript.js:4
2 setTimeout(() => {
3 debugger;
> 4 console.log('world');
5 }, 1000);
6 console.log('hello');
debug> repl
Press Ctrl + C to leave debug repl
> x
5
> 2+2
4
debug> next
< world
break in myscript.js:5
3 debugger;
4 console.log('world');
> 5 }, 1000);
6 console.log('hello');
7
debug> .exit
有关更多信息,请查看debuggers official information