简体   繁体   English

NodeJS Express命令行交互

[英]NodeJS Express Command Line Interaction

When running an Express app, is it possible to interact with its JS environment via command line in a similar manner as a browser's console? 运行Express应用程序时,是否可以通过命令行与浏览器控制台类似的方式与其JS环境进行交互?

For example, say Express is running and I'd like to spot-check variables by printing them to the cli using normal js. 例如,说Express正在运行,我想通过使用普通js将变量打印到cli来抽查变量。 So I'd enter something like: 因此,我将输入以下内容:

 <terminal command> console.log( variableToPrint );

Kind of, you can use a program called 'Debugger' and print every variable value as you want even in the broser, execute this command: 某种程度上,您可以使用一个名为“ Debugger”的程序并根据需要打印每个变量值,即使在broser中,也可以执行以下命令:

node --inspect-brk index.js 节点--inspect-brk index.js

And then go to your chrome broser in the url: chrome://inspect . 然后转到网址chrome://inspect中的chrome broser。 If you just want to use the command line without the broser, run the debugger with this file and the next command: 如果只想在不使用broser的情况下使用命令行,请使用此文件和下一个命令运行调试器:

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

$ node inspect myscript.js $节点检查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

For futher information check the debuggers official information 有关更多信息,请查看调试器的官方信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM