For Node.js applications, why 4000 port doesn't work and 3000 works? - node.js

I have a Node.js/Express.js app running on my server that only works on port 3000 and I'm trying to run with port 4000 , it doesn't work ,what is the reason can anyone explain?Below is the code
Code that didn't work:
console.log("now listening to requests on port 4000");
})```
Code that worked :
```app.listen(3000, function () {
console.log("Express server listening on port 3000");
});```

Related

Node 12.14.1 port problem. Does not take port even after shutting the server down

Why node version 12.14.1 not leaving the specified port even after shutting the server down and also its not using the other port if 3000 is not free. I have to manually kill the process on the specified port
const server = http.createServer(app);
server.listen(port, () => {
console.log("App listening to port" + port);

How to run nodejs on my public ip in windows?

I using windows 10 and want to run nodejs on port 3000 in my public address (just of me and for remote debugging).
So I open the port as say by this answer and I run this code from expressjs
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Then I get myip address, and tell my friend to open http://256.266.266.266:3000/ in his chrome browser.
But the answer is This site can’t be reached ERR_CONNECTION_TIMED_OUT. why? how do I connect to my nodejs application in my public ip?
I do not have any antivirus or firewall as far as I know.
Telnet telnet 256.266.266.266 3000 say .Could not open connection to the host, on port...
netstat -an | find "3000" say TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING

How do I set node app to run on a static port number on heroku?

my node app runs on port 8083 locally. when I push to heroku, how can I configure the service to run on the same port?
You cannot do it. Your code should listen on the port that you have in the PORT environment variable passed to you by the Heroku server that you can access as process.env.PORT and Heroku will listen on the outside on port 80 for HTTP and 443 for HTTPS.
See the docs:
https://devcenter.heroku.com/articles/runtime-principles
In particular:
https://devcenter.heroku.com/articles/dynos#web-dynos
https://devcenter.heroku.com/articles/http-routing
https://devcenter.heroku.com/articles/ssl-endpoint
Correct example:
// Get the port:
const PORT = process.env.PORT || 3000;
// Listen on the port:
app.listen(PORT, () => console.log('Listening on', PORT));
The default (3000 in this example) is for situations when you run it outside of Heroku (like for testing). When it is run on Heroku it should always listen on the port provided by Heroku. If it listens on some other port then Heroku will not proxy the traffic to your app correctly.

Nodejs port 8000 ,4000 not working on server

I am using nodejs for rest-api and my app running on 2000,3000 but when i use port 8000 it work on browser but it not working on iphone also i was opened port 8000 from whm.
Try to run your application on 0.0.0.0 ip address, this guarantees it would be listening on all the configured network interfaces
var server = app.listen(8000, '0.0.0.0', function () {
var host = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});

Use a node app from another device locally

I have a server (10.0.0.12) and my laptop (10.0.0.2) on a local network.
When I run curl http://10.0.0.2:3000 on the server, it works fine. When I run curl http://10.0.0.12:3000 on my laptop, it doesn't work saying site is unavailable.
I am able to ping and ssh into the server from my laptop.
Here is my code to finish the connection:
app.set('port', (3000));
app.listen(app.get('port'), function(){
console.log("Node app running on localhost:" + app.get('port'));
}
I've tried passing in an ip address to the listen() function, but made no difference. I tried passing in 10.0.0.12 (the ip address of the server), 127.0.0.1, and 0.0.0.0 all with the same result.
How can I host my node app on a local network and have everyone who is on the local network be able to access it through the browser?
EDIT: I'm running on CentOS 7.
EDIT2: When I run netstat -lnt, it says this:
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN
Have you tried to just omit the IP address? It should then be available on the IP address of the machine it is running on and the specified port.
As suggested by HA. remove the IP.
As you can see from the documentation:
https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback
If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).
P.S. Which is the OS on the server?
Maybe you can try :
app.listen(3000, '0.0.0.0', function(){
console.log("Node app running on 0.0.0.0:3000");
}
A possible issue could be you aren't using the http module?
var http = require('http').Server(app);
http.listen(3000, function () {
console.log('App running on port 3000');
});
A good practice would be set the port like
app.set('port', (3000));
var http = require('http').Server(app);
http.listen(app.get('port'), function () {
console.log('App running on port ' + app.get('port'));
});

Resources