ThomasReggi ThomasReggi - 5 months ago 37
Node.js Question

Output to Chrome console from Node.js

I'm looking for a way to output Node variables directly into the google chrome browser console. The same way a

console.log()
works on the client side. Something like this for php. This would greatly speed up development.

Answer

NodeMonkey is really awesome. (I agree with honzzz)

1. It's VERY, SUPER simple to use:

All you need to do is include this in your node.js file (after installing it with "npm install node-monkey"):

require('node-monkey').start({host: "127.0.0.1", port:"50500"});

(If you don't specify host and port, default is host/port pair is : http:// 0.0.0.0:50500)

Then go to http:// 127.0.0.1:50500, and open the browser console (Chrome in my case). All console output will be logged there.

2. Starts automatically with your application

You don't have to open another command prompt to start it up

3. You don't have to refresh the browser every time you restart your application

Once you fire it up, you can leave that console open all the time, and do whatever with your node program, and it will re-connect every time you restart it.

(only drawback is that if stop your application for a long time, your console will be polluted with error messages, which you can clear out, of course)

EDIT:

I have switched to node-inspector and I think it's a better choice. Even though the console messages are not output-ed to the Chrome console, it provides a better code inspection using break-points and watches.

It's also easy to use:

After installing it from NPM globally, (npm install -g node-inspector)

1. start debugger in a command prompt using the following:

node-inspector &

2. start your node application in another command prompt with the following:

node --debug yourNodeProgram.js

3. go to http://127.0.0.1:8080/debug?port=5858 in the browser and setup breakpoints, watches, etc just like you would do a front-end application.

The good part is that everytime you restart your node application, you can leave the node-inspector on. After every restart you can just refresh the node-inspector page in the browser.

Comments