Last week, I focused on developing and deploying the front (see here). This week, I started to work on the back-end web services. We all have our own unique way of diving into and learning new things. For me, when it comes to learning software, I find myself heavily rely on the ability to step through the codes in the debugger.
Of course in order to debug web services, you also need a “client” to call your web services. To achieve that, you can either develop dedicated front end applications that calls your web services. Or you can use tools such as postman to call your web services through POST/GET, etc. For the current post, we will focus only on how to launch the debugger assuming that some clients will be calling the web services.
Here’re two alternative options I found useful to start the debugger for web services developed in node.
Debug in Visual Studio Code (My preference)
Select “node” in the initial setup. Visual studio code will launch your node server in the debug mode. From there, you have all the usual debugging functionalities available for you, e.g. setting up break points, step through/in/out, inspecting call stack, etc.
Debug in Chrome
If you to use Chrome or other tools to debug, you can also change node script in package.json to add the “- - inspect” option, e.g.
“start”: “node — inspect index.js”
You can then do “npm start” and start your server as usual (but with debugger listening in the default port of 9229).
> node — inspect index.js
Debugger listening on ws://127.0.0.1:9229/01b522be-e2a3–474a-8bf2-a009fe7f858b
From there, you have two options:
Option 1: go to chrome://inspect and click Configure, add the 127.0.0.1:9229. Once the node appears in the Target list, click “inspect” and it will launch the chrome devtools where you can add your folder and start debugging.
Option 2: go to http://127.0.0.1:9229/json and copy the devtoolsFrontendUrl from the output. You can past it in the chrome browser (chrome-devtools://devtools/bundled/XXX see below for the full command) and that will launch the devtools as well.
I am sure there’re other tools and approaches to debug web services. Whichever you prefer, debugger is not just for debugging issues but also an effective learning tool.