简体   繁体   English

服务器运行时Node.js javascript命令行?

[英]Node.js javascript command line while server running?

If I have a node.js web server running with express, is there a way to get a command line?如果我有一个运行 express 的 node.js Web 服务器,有没有办法获取命令行? Kind of like the way the browser has a javascript console.有点像浏览器有一个 javascript 控制台的方式。 I want to type in variables and inspect them in node.js runtime.我想输入变量并在 node.js 运行时检查它们。

If I type in 'node' then it gives me a command line.如果我输入'node',它会给我一个命令行。 but if I type in 'node server.js' that command line is not available.但是如果我输入“node server.js”,该命令行将不可用。 How can I make the command line always available (in bottom of screen) regardless of the server's state?无论服务器的状态如何,如何使命令行始终可用(在屏幕底部)?

To do this type node --inspect server.js要执行此类型node --inspect server.js

Then to connect to it type in chrome://inspect/ in url bar of Chrome.然后在chrome://inspect/ url 栏中输入chrome://inspect/连接到它。 Then click "inspect" on that instance under Remote Target.然后在远程目标下的该实例上单击“检查”。 Then I found I can see variables/objects that are under the global namespace.然后我发现我可以看到global命名空间下的变量/对象。

Also, a very basic way to add keyboard interaction on the server window itself without having to connect to it:此外,一种在服务器窗口本身上添加键盘交互的非常基本的方法,而无需连接到它:

var readline = require('readline');
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);

setInterval(function() {
    console.log('server stuff');
}, 1000);

process.stdin.on('keypress', (str, key) => {

    if(key.sequence === '\u0003') {
        console.log('ctrl-c pressed');
        process.exit();
    }

    console.log('you pressed: ' + key.name);
});

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

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