Search code examples
javascriptnode.jsvue.jsgoogle-chrome-devtoolsnode-debugger

How debug a nodejs API


I've been worked on a vue project.

This vue project use the nodejs API I've created, in simple way, they are two entire differents project which are not located in the same directory and they are launched separately.

The problem is whenever I debug a route with node --inspect --debug-break event_type.controller.js for example named:

"/eventtype/create"

exports.create = (req, res) => {
  const userId = jwt.getUserId(req.headers.authorization);

  if (userId == null) {
    res.status(401).send(Response.response401());
    return;
  }
  // Validate request
  if (!req.body.label || !req.body.calendarId) {
    res.status(400).send(Response.response400());
    return;
  }

  const calendarId = req.body.calendarId; // Calendar id

  // Save to database
  EventType.create({
    label: req.body.label,
  }).then((eventType) => {
    Calendar.findByPk(calendarId).then((calendar) => {
      eventType.addCalendar(calendar); // Add a Calendar
      res.status(201).send(eventType);
    }).catch((err) => {
      res.status(500).send(Response.response500(err.message));
    });
  }).catch((err) => {
    res.status(500).send(Response.response500(err.message));
  });
};

Even if I create a breakpoint on const userId = jwt.getUserId(req.headers.authorization);

and from my vue app I trigger the api createEventType event, my break point is not passed.

Also when I press f8 after the breakpoint on my first line with the debugger, my file close automatically.

I do not use VS Code but Vim for coding but I've heard that maybe Vs Code could allow a simplified way to debug nodesjs application.

NOTE: I use the V8 node debugger.


Solution

  • The solution (even if it's not really a solution) has been to add console.log to the line I wanted to debug.