How can i debug a nodejs backend with node-inspector? - javascript

I am trying to debug my backend and i am not really sure if i am doing it the correct way.
I have set up the debugging as follows:
Node 6.9.1 and node-inspector 0.12.8
open a command prompt and run the following command:
node-inspector --web-port=3030 (server app port)
open another command prompt and run the following command:
node --inspect --debug-brk server.js
browse to the given URL in the second command prompt log on screen
press F8 to make the server run
eventually put some others breakpoints
browse on another tab to your app (address and port defined in 1-)
see server execution stops on breakpoints defined on step 5.
Now when i run node --inspect app.js and everything looks good so far. I can debug the first start in the app.js. But if I want to debug an endpoint with POSTMAN, I get the error "Cannot POST /api/trainingsWeek". The endpoint works if I don't debug.
Do I have to take another address? Or another tool than POSTMAN?
ANd what is the difference between node --inspect app.js and node-debug
GitHub Issue
UPDATE
This was my stupid mistake :P Here is the solution: https://github.com/node-inspector/node-inspector/issues/907#issuecomment-280620108

I use VS Code to debug. I guess you don't need node-inspector, it will run with the integrated debugger from Node. To get it running, open your project and the debug view. There should be no configuration available. Click on the little wheel -> choose node.js as the environment.
After that the standard json will be created and looks like this:
And then click on the play button with the selection start programm. With that the debugging should run. Now you can use POSTMAN with the same url when you just run your server and the debugger from VS Code should hit your breakpoint.

Related

Internal Server error using Bash Terminal, any idea why is it throwing this error and how to fix it?

I have just recently started learning about APIs as party of Full stack developer course. Everything has been working fine until I have activated system storage sense in Windows to clear some space in the disk /c, the curl command stopped working and is giving internal server error.
curl calling request error using bash terminal
I have realized there is an issue when I have run my code and it stopped pulling information from the API link.
Since you made a windows change my guess would be - try addding the curl folder path to your Windows PATH environment variable so that the curl command is available from any location at the command prompt.

How to initiate a new Node.js file after already initiating some other Node.js file on Windows 10 machine?

I've a laptop running on Windows 10 Home Single Language 64-bit Operating System
I am learning Node.js on this machine.
For downloading and installing Node.js I went to the URL
https://nodejs.org.
From there I downloaded the Node.js setup file v6.10.3 LTS for Windows (x64)
I started installing Node.js by double-clicking on the setup file and keep on clicking the Next button in the installation wizard.
Then, I clicked on the Windows Start button(the button appearing in bottom-left corner of the screen), two new options were appeared in the list of 'Recently Added' as Node.js and 'Node.js command prompt'
I clicked on the option 'Node.js command prompt', then following screen appeared.
Then, I created one file titled myfirst.js at location
C:\Users\lenovo\myfirst.js
Then, I initiated this file by typing in following text at the command prompt I opened earlier :
C:\Users\lenovo>node myfirst.js
Then, I run the file myfirst.js from the browser by hitting the URL :
http://localhost:8080/
Till now everything worked fine for me.
But real problem started when I wanted to run another program.
For it do I need to close the command prompt by clicking on the close button icon appearing on the top-right corner of the window and should I need to do this before running any new Node.js program to initiate the respective file?
The screen I'm having problem with is appears as below with the name of earlier run Node.js program(i.e. with the last initiated Node.js file) :
Someone please help me in this regard.
Thank you.
So, I'm not on Windows, but in general each node application will run in a single process by default. If you're running it from a command prompt, that will default to running the process in the foreground (meaning, it consumes the same process as your command window) and you have to open a new command window to run another program. You can either close that first window, or run the node application in the background.
You'll also want to make sure you're not trying to do two programs listening on the same port (i.e. two node apps both running on port 8088), as that's also not possible.
The node.js file you created is set up to listen to port 8080 and will do so until you "stop" the program from running. To do this, press CTRL-C. That will return you to the command prompt. To run your next program, initiate it with node filename.js.

Why is the error happening?

I'm following this tutorial Full-stack Redux Tutorial and everything went well till the moment I had to run a local server (Starting under the title "Setting Up a Socket.io Server"). I copied exactly what the tutorial shows and I'm getting this error when running "npm run start"
As seen in the image, the command I'm trying to run is:
babel-node index.js
But the error says nothing I can catch, just that something is wrong with the command, not even with a file.
I'm lost and Google offers little help.
Something else is listening on port 3333 on your machine. Change the port number to something else, and it should work.
If you told us what OS you were using, we could suggest how to determine what is listening on port 3333.
It seems like there's still a connection open on port 3333. If you open cmd and run netstat -a -b you should be able to get a list of open connections together with the executables that started them.
It might be that your Socket.io server previously crashed and the exception wasn't handled properly, possibly leaving the connection open?

what's the right process to debug with node-inspector?

I'm on node version v5.0.0. I installed node-inspector version: v0.12.5.
what I did:
1st. I start node-insepctor on port 8090 (due to 8080 is taken by nginx)
node-insepctor --web-port=8090
2nd. I start debugging my js file
node --debug-brk app.js
3rd. I open the url in browser
http://127.0.0.1:8090/?ws=127.0.0.1:8090&port=5858
It works for the 1st time, it will perfectly break at the first line. But after I find and fix the issue in my code and I want to debug again, it just can't works again.
What I tried:
1st.
I see that in the console, the following line which printed when I node --debug-brk app.js is still there:
Debugger listening on port 5858
So I refresh the page, the code is shown, but it just doesn't get executed, so no break point is reached.
2nd. I stop the app.js debug process by CTRL-c, and restart debugging app.js, Debugger listening on port 5858 is shown again. so I open the url again. same as above, the code is shown, but nothing happens.
3rd. I stop both the node-inspector process and app.js debug process and restart them both and open the url. this time, it works!!
Is there an easy way to debug a js program more than once??
Every time I need to kill both node-inspector process and debug js process to make it work again??
Or I'm doing something wrong here??
Please help!
node-inspector can be executed with the node-debug <file> command which adds the --debug-brk option by default. This opens the file in the Sources tab and breaks on the first line.
To continue debugging, you'll need to follow the steps below:
Select the file you need to debug from the Sources tab.
Click on a line number of the file to set a breakpoint.
Resume script execution (F8). This will cause the execution to break on the line that was highlighted in step 2.

Failing to debug with node-inspector - what is wrong?

I have a javascript code that checks whether a point is inside a polygon. I am trying to debug it with node-inspector, following these steps:
In the first window:
PS Z:\dev> node-inspector.cmd
info - socket.io started
visit http://0.0.0.0:8080/debug?port=5858 to start debugging
In the second window:
PS Z:\dev\poc\SDR> node --debug-brk .\IsPointInside.js
debugger listening on port 5858
Now I navigate to http://localhost:8080/debug?port=5858 in my Chrome browser.
What happens is that Chrome gets stuck waiting for localhost presenting me the empty screen.
I must add that I have successfully debugged the r.js javascript optimizer using the same steps before, but now I cannot debug it as well.
according to this there seems to be an order in which the components should be started.
Its application -> debugger -> web browser.
Could be your issue.
You can also send a "debug on demand" command. Use this command:
kill -USR1 $PROCESSID

Categories