How to debug API

Published at: July 2, 2018, 3:51 a.m. in the Documentation

For local debugging of your API, you can use Inspector, you can debug both in the terminal without installing any more dependencies, or debug in third-party Inspector Clients, or even remotely via WebSocket.

Debugging in the terminal

In the simplest case, run the debugger like this:

% node inspect `which smappi` run 8001

< Debugger listening on ws://127.0.0.1:9229/aba67168-d486-4196-b8f2-e0cb36bc082a
< For help see https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in /usr/local/lib/node_modules/smappi/bin/smappi.js:1
> 1 (function (exports, require, module, __filename, __dirname) {
  2
  3 /* eslint no-console: 0 */
debug> c
< Smappi Dev Server running at http://localhost:8001/
debug>

Type "help" in the "debug>" prompt and see the list of features, and also read the official debugger documentation.

Running the Debugger with GUI

You can start the debugger in graphical mode, for this you need to start the WebSocket channel and use any compatible Inspector Client for this, for example NiM for Google Chrome:

% node --inspect `which smappi` run 8001

Debugger listening on ws://127.0.0.1:9229/4602f9b7-d725-4c9a-bbe0-d1ee86c04f68
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
Smappi Dev Server running at http://localhost:8001/

After you have installed Chrome Extension NIM (Node Inspector Manager), click on its icon and make sure that the settings are correct:

Below is an explanatory video from the authors of NiM:

Debugging

Now you can set breakpoints in your code, for this you can enter the keyword "debugger" anywhere in the code and the debugger will stop at it:

const { jquery, request, DOM, logger: console } = require('smappi-cl');

function example (URL) {
    let $ = jquery(DOM(request.get(URL).content));
    debugger; // << BREAKPOINT HERE
    $('div[class~="item"]').each(function () {
        console.log('ITEM', $(this));
    });
}

module.exports = { example }

and also, you can specify breakpoints directly in the interface of your debugger:

if you have previously debugged NodeJS projects, then you have not learned anything new.