【问题标题】:NodeJS Express Command Line InteractionNodeJS Express 命令行交互
【发布时间】:2018-02-09 13:18:54
【问题描述】:

在运行 Express 应用时,是否可以像浏览器控制台一样通过命令行与其 JS 环境进行交互?

例如,假设 Express 正在运行,我想通过使用普通 js 将变量打印到 cli 来抽查变量。所以我会输入类似的内容:

 <terminal command> console.log( variableToPrint );

【问题讨论】:

    标签: javascript node.js express command-line terminal


    【解决方案1】:

    有点,你可以使用一个叫做“调试器”的程序,甚至在broser中打印你想要的每个变量值,执行这个命令:

    节点 --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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多