Starting node http server at any free port - node.js

I am trying to start multiple highchart export servers via C# in a single windows machine. Highchart export server is run as node http server. All node servers are started from different non communicating applications running in a single machine. I am starting the process from c# code like this.
Process process = new Process { command line arguments to start node server with port };
process.Start();
How can I specify non conflicting ports in C# code so that node servers can be started in each application ?

Node.js can help you, use c# no problem.
You will need just to check port before you use it. If port is used you can't use that port this is basic rule for TCP/IP.
Take a look here :
NodeJS pinging ports
Also npm have some app for port checking.
https://www.npmjs.com/package/tcp-ping
Port is just a number you can use it on your will but would be better to follow standards. You can use 80 or 21 if port are free .

Related

Nodejs application run without port

I've developed a few Node.js programs that operate on various ports.
Is there a way to use the paths http://localhost/node1 and http://localhost/node2 to run without port access?
If you want your app to be accessible over http protocol then your app needs to listen on a port.
If you just want to avoid using port when you type the url in the browser you need your app to listen on port 80 which is used by browser as default so can be omitted.

Express server unreachable from browser when running in WSL2

So, I have been trying to start a basic express server from inside WSL2 on port 3000. Every time I go to the browser, it says that "localhost:3000" is unreachable. I'm not really sure where the problem is. I have been able to start and access react applications that are running on port 3000 in WSL2 without a problem. Additionally, I have been able to start Flask servers from other ports on my machine from within WSL2 without a problem. The issue somehow seems isolated to Express JS servers, which doesn't make a whole lot of sense to me.
I have confirmed that the app is exposed on all interfaces, so localhost should be reachable.
When starting the application, I have been monitoring it with netstat -ltp. The node process does show up in the output, which indicates that the port is listening.
Are there any weird WSL2 configuration steps that I missed. The strange part is that other applications that don't use Express JS, applications seem to be running perfectly fine on all ports.
I also already have all local ports accessible through the Windows Firewall and have made the ports accessible on the Windows side via this command: netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=127.0.0.x
I have also been testing this using npx express-generator your-project-name --no-view to create a standard Express JS template.
Update:
The server is reachable from the IP address on my network, but unreachable from localhost.

Do I need a different server to run node.js

sorry if this is a wrong question on this forum but I am simply just stuck and need some advice. I have a shared hosting service and a cloud based hosting server with node.js installed. I want to host my website as normal but I also want to add real time chat and location tracking using node.js I am confused with what I am reading in several places because node.js is itself a server but not designed to host websites? So I have to run 2 different servers? One for the website and one to run node.js? When I setup the cloud one with a node.js script running I can no longer access the webpages.
Whats the best way for me achieve this as I am just going round in circles. Also is there a way I can set up a server on my PC and run and test both of these together before hand so I see what is needed and get it working as it will stop me ordering servers I dont need.
Many thanks for any help or advice.
Node can serve webpages using a framework like Express, but can cause conflicts if run on the same port as another webserver program (Apache, etc). One solution could be to serve your webpages through your webserver on port 80 (or 443 for HTTPS) and run your node server on a different port in order to send information back and forth.
There are a number of ways you can achieve this but here is one popular approach.
You can use NGINX as your front facing web server and proxy the requests to your backend Node service.
In NGINX, for example, you will configure your upstream service as follows:
upstream lucyservice {
server 127.0.0.1:8000;
keepalive 64;
}
The 8000 you see above is just an example, you may be running your Node service on a different port.
Further in your config (in the server config section) you will proxy the requests to your service as follows:
location / {
proxy_pass http://lucyservice;
}
You're Node service can be running in a process manager like forever / pm2 etc. You can have multiple Node services running in a cluster depending on how many processors your machine has etc.
So to recap - your front facing web server will be handling all traffic on port 80 (HTTP) and or 443 (HTTPS) and this will proxy the requests to your Node service running on whatever port(s) you define. All of this can happen on one single server or multiple if you need / desire.

Resolving port issue of node js application and windows server

Introduction :
My windows server is listening at port 3000.When i start my node application to use port 3000 for listening, it failed with error.
I understand that i have alternate method of deploying node app using iis but i failed.The only way left is making problem i.e "port".
Edit
When i unbind the port, i app start working.
If someone have better idea or idea about this problem, please do help. Thanks for your time.
Only one server at a time can use a given port. If your windows server is already using port 3000, then you can't start another server on that same port. You will have to either stop the first server or pick a different port number.
Or, use some sort of proxy as your only listener on port 3000 and have it divide the traffic among your two servers which would each run on different ports.

How to set up Node server for production on own machine?

This must be a pretty basic thing to do, but I cannot find any good guide on how to do it on the internet. I only find how to set up a development environment for Node. I want to be able to forward my R-Pi's port 80 to my Node server, which I want to obviously listen on port 80. How can I close the native port 80 so that I can let me Node server listen on that port.
Ultimately, I want to be able to access my pi from any remote location. I know how to set up a static IP and forward the port on my router, but now how do I allow Node into port 80?
Two options. Either disable any other service running on port 80 and run Node with sudo. Or setup something like nginx to forward traffic from port 80 to your Node instance. To do that you can open a socket file with node and configure nginx similar to https://github.com/trevnorris/norrd/blob/master/conf/nginx.conf
Creating the socket is simple enough. It's as simple as
net.createServer(function(){}).listen('/path/to/file.sock');
I usually opt to spawn child processes for easier monitoring from the parent process, but use nginx to connect to each socket. For two reasons, one is it's easy to set route static content around the node process, and also because I prefer not to setup my own access privileges from scratch.

Resources