Nodejs port 8000 ,4000 not working on server - node.js

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);
});

Related

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

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");
});```

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

Openshift - port to use on deployment

I have the following start.js file:
var express = require('express');
var app = express();
app.use(express.static('static'));
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
In my NodeJs application on Openshift. However, when I run rhc tail-a app-name
I can see that there is an error of :
Error: listen EADDRINUSE :::8080
I've tried 80 and 443, and received those errors:
Error: listen EACCESS 0.0.0.0:443
Or 80
Which port should I use as default on my app?
Thanks!
Use Nginx,
Nginx (pronounced "engine x") is a web server. It can act as a reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer and an HTTP cache.
It isn't good practice to run your application with root privileges or directly run your application on port 80 and your port 8080 is in use. Try different port and use reverse proxy.
But if you want to run on port 80 or 443, run your application with root privileges.

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