Sunday, August 23, 2020

Docker Assignments #15: Debug Node.js and Express.js applications using Chrome DevTools

Node.js process listens for a debugging client by enabling --inspect (or) --inspect-brk. By default, it will listen at host and port 127.0.0.1:9229

Ex: >  node --inspect server.js   (or) 
      >  node --inspect-brk server.js   (i.e. it stops execution at the start of the script )

Create a folder structure as per below image in the host machine
  

  1. Add "express" npm package as a dependency in package.json file
  2. configure npm debug command as "node --inspect-brk index.js" in package.json
  3. Write required code into index.js file to expose GET endpoint using  express.js
  4. execute "npm run debug" command in the terminal
  5. Open Chrome DevTools  by typing "chrome://inspect" in address bar and click on "Open dedicated DevTools for Node"
Step 1:
         > npm i express

Step 2:
        "scripts": {
    "debug""node --inspect-brk index.js"
  }


Step 3:
const express = require("express");
const app = express();

app.get('/', (req, res) => {
    res.send("sample express GET end point")
})

app.listen(9300, () => {
    console.log("app listening on port 9300");
})

Step 4:
      > npm run debug

Step 5:
      see the following chrome DevTools window

Chrome DevTools has following major features
  • break-point debugging
  • JavaScript live Edit
  • souremap file searching
  • JavaScript profiler with flame-chart
  • Heap snapshot inspection, heap allocation time line and profile
Happy Coding :)

No comments:

Post a Comment