Node.js javascript command line while server running?

Viewed 252

If I have a node.js web server running with express, is there a way to get a command line? Kind of like the way the browser has a javascript console. I want to type in variables and inspect them in node.js runtime.

If I type in 'node' then it gives me a command line. but if I type in 'node server.js' that command line is not available. How can I make the command line always available (in bottom of screen) regardless of the server's state?

1 Answers

To do this type node --inspect server.js

Then to connect to it type in chrome://inspect/ in url bar of Chrome. Then click "inspect" on that instance under Remote Target. Then I found I can see variables/objects that are under the global namespace.

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);
});
Related