2017-08-31 105 views
0

當運行一個Express應用程序,是否有可能以類似的方式作爲瀏覽器的控制檯通過命令行的JS環境進行交互?快速的NodeJS命令行交互

例如,假設快速運行,我想他們打印到使用正常的js CLI來抽查變量。所以我輸入類似:

<terminal command> console.log(variableToPrint); 

回答

0

的,你甚至可以在broser,只要你想使用一個名爲「調試」和打印每個變量值的程序,執行此命令類:

node --inspect-brk index.js

然後去url中的你的chrome broser:chrome://inspect。如果你只是想用命令行沒有broser,運行這個文件,下一個命令調試器:

// myscript.js 
global.x = 5; 
setTimeout(() => { 
    debugger; 
    console.log('world'); 
}, 1000); 
console.log('hello'); 

$節點檢查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