Output to Chrome console from Node.js

前端 未结 6 974
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 16:40

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. Somethin

6条回答
  •  粉色の甜心
    2020-12-09 17:04

    For users with nodejs on linux via ssh-shell (putty):

    Problem with nodejs on linux-ssh-shell is, that you have no browser connected. I tried all this solutions, but didnt get it to work.

    So i worked out a solution with firebase (https://firebase.google.com), because my project uses firebase. If you are familiar with firebase, than this is a great way. If not, firebase is worth using in combination with nodejs - and its free!

    In the server-side-script (started with node) use a own function log():

    // server-side:
    // using new firebase v3 !
    var fbRootRef = firebase.database();
    var fbConsoleRef = fbRootRef.ref("/console");
    var log = function(args) {
      fbConsoleRef.set({'obj': args});
    }
    
    // inside your server-code:
    log({'key':'value'});
    

    On client-side you create a firebase-reference on this console-object:

    // client side:
    fbRootRef.child('/console').on('value', function(d) {
      var v = d.val();
      console.log(v);
    });
    

    Now everything logged on server-side with the log() - function is transferred in realtime to the firebase-database and from there triggering the client-console-reference and logged into the browsers console.

    If anyone needs help, i will explain in more detail and could give a more extended version of this logging with types (console./log/warn/info), grouping with title-info (i.e. server says: (filename + line).

    Setting up firebase for your project is done in max 30 minutes, inserting the console-function in 30 minutes. I think its worth the time!

提交回复
热议问题