I’ve been coding on servers using Node.js for a few years, and the way I’ve been debugging my code was using console.log('>>> BLA: ', JSON.stringify(...))
. How many times have you done that? π€
Until more recently I came across a feature while using Microsoft Visual Studio Code: Debugging my code with breakpoints!! π
As you can see in the video:
- I have my server running (inside a separate terminal window). An iOS app running on the side.
- First, I click that βΆοΈ button in Visual Studio Code to start the debugging session, and pick the process that my server is on. (more on that later)
- That attaches to the existing process, which is the separate terminal window where your server is running.
- When I trigger the api call from the iOS app, I hit my customer server endpoint.
- VoilΓ ! The control is transferred to Visual Studio Code, where you can inspect all kinds of variables and step through the code! π
Benefits
- No more console logging (OMG π )
- You can attach to the server at any point, without having to terminate it first and re-launch.
- Enjoy faster and happier coding π¨π»βπ»
Setup
- Hold CMD + P to bring the file navigator.
- Type: `debug `, it will prompt you to select from the list of projects you have in the workspace.
- Pick your server project (in my case rest-api), which will create a
launch.json
file. - I selected the βAttach to Processβ option.
Content of my launch.json
file:
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "node", "request": "attach", "name": "Attach by Process ID", "processId": "${command:PickProcess}", "skipFiles": [ "/**" ], "port": 10000 } ] }
- Now you can click the βΆοΈ to attach to your existing server process.
- You can then detach, then reattach if needed.
I hope you found this post useful, please subscribe for more content π»
Ledger Manager
Check out my iOS app which facilitates recording crypto transactions (Bitcoin, Ethereum, etc.), simplifies tax reporting & reports on overall net worth!
*Everything is synced privately on your iCloud account.
**Helps getting your crypto tax reporting in order (Form 8949) with smart calculation of cost basis & acquisition dates.