How to debug and log own code on the server side of Meteor?

你。 提交于 2019-12-03 01:13:37
Qichao Dong

Now that I know how to do this, I will answer my own question so that we can keep this information (in details) here: (This is based on Mason Chang's answer to this question.)

  1. Stop meteor execution.
  2. Edit /usr/lib/meteor/app/meteor/run.js (or the corresponding run.js installed by meteorite in HOME//.meteorite/meteors/meteor/meteor/[LONG_HEX_CODE]/app/meteor):
    change the line
    [path.join(bundle_path, 'main.js'), '--keepalive']
    to
    ['--debug-brk', path.join(bundle_path, 'main.js'), '--keepalive']
    //--debug-brk makes the new thread break at the first line;
  3. Add debugger statements as breakpoints in your server code;
  4. Run node-inspector & in a server terminal. (google "node-inspector" to install it.)
  5. Run meteor; (this will not have the debugger attached as there's no server thread yet, if you have no client window open.)
  6. Refresh client browser window; (to initiate a server thread that will break at the first line, and be attached to node-inspector.)
  7. Open a browser window at [SERVER:8080], your server code stops at first line (main.js in your [PROJECT_DIR]/.meteor/local/build);
  8. Hit the RUN button on the debugger browser window; depending on where your debugger statements are, you may have to do some triggering actions in client browser window to run to the debugger breakpoints. (Note that if you wait too long to hit the RUN button, your client window may time out, and you have to refresh again.)
  9. Now you can do the usual debugging stuff in server debugger window: step through, watch variables, execute in console, look at the stack, etc.

Edit: For logging on server side, you can use either Meteor._debug() and console.log(), they will show up in the terminal where you run meteor. On client side, these logging statements will output to the console of your browser's dev. tools.

Laurent Roger

On MacOSX, you can use with Chrome :

NODE_OPTIONS="--debug-brk" meteor

and in another terminal

node-inspector --debug-port=5858 --web-port=12345

Then connect Chrome to 127.0.0.1:12345/debug?port=5858

Otherwise with Webstorm, just create a Node.js Remote Debug configuration and run it :

Name : Meteor
Host : 127.0.0.1
Port 5858

Note that once the server has started, you need to press run in order for Meteor to load, and then pause in order to debug from the server console.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!