WebSockets request was expected error when using --inspect-brk option - node.js

When I run nodemon dist/server/app.js it works on default port 3000 and I'm able to reach my API. But if I run
nodemon --inspect-brk=localhost:3000 dist/server/app.js
I got error
WebSockets request was expected
What's wrong?

You can't run your web server and the debugger on the same port. They are each separate servers (the debugger is a server built into the node.js runtime).
So, you can either remove the port and host designation from the --inspect-brk option and just let it use the defaults (which is all I ever use) or you can select a different port for the debugger that doesn't conflict with your web server or anything else running on that host.

Related

Node.js / curl(7) failed to connect to localhost port 3000. How to fix it?

response from testing
code hello.js
i am trying to create a web service with node.js. Unfortunately after implementing the application hello.js i am only able to test successfully "node hello.js", but furthermore when i type in "curl http://localhost:3000" i am getting "curl: (7) Failed to connect to localhost port 3000: connection estavlishment rejected"
This also happens with other ports oder when i type 127.0.0.1 directly. I also disabled my firewall with ufw and allow 3000 with ufw.
I am working with VirtualBox for a uni project. I am running everything in the VM. The goal is to cluster with docker and i need the web server for loadbalancing in the end.
Thanks!
The issue here that you use same terminal for multiple processes. As soon as you start the server, it holds the terminal. It means when you press Ctrl + C (^c in terminal output), you stop the server.
In your case you should use two terminals. In the first one you start the server (and do nothing with this particular terminal), in second one you make the request. Or you can change second step and make request from browser (it will be GET request by default), Postman, Insomnia etc.

Change port if specific port is already used

If I try to run the react app in one terminal, it opens on port 8000, and if I open a new terminal tab and try to run the app again I get the following, after which it opens the app on port 8001
Something is already running on port 8000.
Would you like to run the app on another port instead?(Y/n)
And this is possible because of:
"start": "react-scripts start"
Most of the solutions suggest to kill the process that occurs in the port.
But I'm trying to implement this sort of function for a Node(Express) application, such that if a particular port is running/in use, it should just open the app in another port.
I've also thought of this, as a solution, but this would only let me open the app either in 8000 or 8001. I want the app to be able to open on 'n' number of terminals with 'n' different ports. Essentially, is there a NodeJS alternative to "start": "react-scripts start" ?
Using express you can omit the port params when calling app.listen, which you make express run your app on a random unused port.
Doc: https://expressjs.com/en/4x/api.html#app.listen
EDIT:
The solution to the problem can be found here

How does a react app can be set up on server

I'm trying to understand what needs to be done to put my react app online.
Until now, I launched it on my mac using npm start, and accessing localhost:3000 or http://127.0.0.1:3000.
So I currently have bought a small server, installed everything (last version of node and npm, git and other necessary things), cloned my repo, and installed all dependencies.
When I do npm start on the server, it says it's available on port 3000. But when I go in my server's ip with the following :3000, it times out.
I don't really understand what need to be done to do this, I found some things about configuring apache on the server, others about using pm2 so have a node script running even after leaving the terminal, but that would be my next step I guess.. And other about configuring things with express (but do I need node+ express here ? As it's a simple front end react page ?).
if you are using webpack devserver, use it for development only
The tools in this guide are only meant for development, please avoid using them in production!
back to your question, there is a difference between binding to 127.0.0.1 or binding to 0.0.0.0
try changing the devserver to listen to 0.0.0.0
webpack.config.js
module.exports = {
//...
devServer: {
host: '0.0.0.0'
}
};
Usage via the CLI
webpack-dev-server --host 0.0.0.0
also note, that you will need to allow ingress rules (incoming connections). that is, allow a request from the internet to reach your server
There are a lot of configurations you will have to do when you deploy your application on a server. Building the app, Nginx, pm2 and even ssl certification. This video is 20min and has all you need. https://www.youtube.com/watch?v=oykl1Ih9pMg&t=1s

Debugging Kibana backend Node.js code

How to debug Kibana backend source code?
I presume, node-inspector could be used. And some extra configuration needed for package.json file to run debugger at npm start. But, I can't figure out the correct configuration syntax.
NODE_OPTIONS=--debug bin/kibana --dev
If you want to break before starting:
NODE_OPTIONS="--debug --debug-brk" bin/kibana --dev
Alternatively, you can set the same variable when calling npm start:
NODE_OPTIONS=--debug npm start
You will see:
Debugger listening on port 5858
You can then use the node inspector by running node-inspector and opening http://127.0.0.1:8080/debug?port=5858.
If you are debugging a remote server, you can either run node-inspector on the server and forward port 8080 through SSH, or you can run node-inspector locally and forward port 5858 through SSH.
EDIT: As it was now mentioned in the comments, it might be necessary to edit the source and add debugger; at the beginning, otherwise breakpoints might not work correctly even when using --debug-brk. Not sure why, though.
add $NODE_OPTIONS --no-warnings --inspect
and start kibana server and access the link shown in the logs like this link "chrome-devtools://devtools/remote/serve_file/#521e5b7e2b7cc66b4006a8a54cb9c4e57494a5ef/inspector.html?experiments=true&v8only=true&ws=localhost:5858/node" then you will find your backend code in the source tab and you can debug it like debuging frontend code.

Meteor always listens on port 3000

I created a new Meteor project in WebStorm on Windows 8.
In "Run/Debug Configurations" I set the port value to 3008, but when I run the app, it always works on port 3000.
Does anybody know where Meteor defines the port number or how can I change it?
I've searched the words "3000", "PORT" and "listen" in the entire project but they don't exist.
I've also seen these questions which didn't help me because they use Express which defines the port number hard-coded:
Node.js/Express.js App Only Works on Port 3000 ,
Express is listening on port 3000 despite setting it to 80?
Any help will be profoundly appreciated!
Try passing -p 3008 as a program argument in Meteor run configuration:
http://localhost:3008 in Browser/Live Edit tab is just used for browser launching (i.e. it tells WebStorm to launch the browser with specified URL), it's not supposed to affect Meteor port in any way

Resources