Can I listen to website url host? Node JS - node.js

I can't understand one thing- does NodeJS allow to listen to custom hostname? Not localhost. Because when I listen to my website url (example.com), I'm getting the following error:
Error: listen EADDRNOTAVAIL example.com ip-address:1000 at
Object.exports._errnoException (util.js:1022:11) at
exports._exceptionWithHostPort (util.js:1045:20) at Server._listen2
(net.js:1246:19) at listen (net.js:1295:10) at net.js:1405:9 at
_combinedTickCallback (internal/process/next_tick.js:77:11) at process._tickCallback (internal/process/next_tick.js:98:9) at
Module.runMain (module.js:606:11) at run (bootstrap_node.js:394:7) at
startup (bootstrap_node.js:149:9)
Why does it happend? And can I listen for POST messages from external site URL?

Why does it happend?
This happens because the hostname and port you requested isn't available to you.
can I listen for POST messages from external site URL?
No, You can't. server.listen() accepts hostname and port
Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections on any IPv6 address (::) when IPv6 is available, or any IPv4 address (0.0.0.0) otherwise. Omit the port argument, or use a port value of 0, to have the operating system assign a random port, which can be retrieved by using server.address().port after the 'listening' event has been emitted.
And further digging into list of Node.js Common System Errors docs and exhaustive list, It is clear that, Address not available was the case.
EADDRNOTAVAIL Address not available (POSIX.1).

Related

Greenlock #v4 don't create ssl sertificate

When i start node js server that are using greenlock i got this message
Listening on 0.0.0.0:80 for ACME challenges, and redirecting to HTTPS
Listening on 0.0.0.0:443 for secure traffic
Ready to Serve:
But 2 minutes later i got error message
Error cert_issue:
connect ENETUNREACH 172.31.240.54:80
code: ENETUNREACH
Error: connect ENETUNREACH 172.31.240.54:80
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16)
I use virtual server and domen that i buy on GoDaddy

Express server errors with external IP, works fine otherwise

I have an Express server - very small at the moment, this is my whole code:
const express = require('express');
const app = express();
const port = 8080
app.get('/', function(req, res) {
res.send('Hey!')
})
app.listen(port, 'my.ip.address')
It works just fine when my.ip.address is localhost/0.0.0.0/127.0.0.1/192.168.0.28 (my network IP.) But when my external IP is provided, I get this:
events.js:298
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL: address not available myip:8080
at Server.setupListenHandle [as _listen2] (net.js:1292:21)
at listenInCluster (net.js:1357:12)
at doListen (net.js:1496:7)
at processTicksAndRejections (internal/process/task_queues.js:85:21)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1336:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EADDRNOTAVAIL',
errno: -99,
syscall: 'listen',
address: myip,
port: 8080
}
I know that 8080 is correctly forwarded, as another example server (Apache) works just fine, and can be connected to from my external IP.
TLDR:
Just listen to the port and remove the IP address:
app.listen(port)
What's happening:
When you do the following:
app.listen(port, 'my.ip.address')
What you are doing is telling your OS that you want to listen to incoming packet from the port on the network card that owns that IP address.
If the OS cannot find any network card (ethernet, wifi etc.) that has that IP address assigned then your OS will error out saying it cannot find the hardware you want to listen to.
Since your external IP is owned by the external network device of your router your OS cannot find the hardware that owns the IP address.
Altenate solution
You can just listen to the IP address assigned to the network card that is connected to your router:
app.listen(port, 'my.local.ip.address')

Amazon EC2 Error: listen EACCES 0.0.0.0:80

I have already added the HTTP TCP Port 80 to the inbound rules, but I still get the error:
Error: listen EACCES 0.0.0.0:80
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at Server.setupListenHandle [as _listen2] (net.js:1338:19)
at listenInCluster (net.js:1396:12)
at doListen (net.js:1505:7)
at _combinedTickCallback (internal/process/next_tick.js:141:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
at Function.Module.runMain (module.js:695:11)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
You probably have to run your node.js script with sudo as you want to listen on port 80.
You cannot run a process that listens on low ports (below 1024) without root privileges.
You either try to run as sudo, as stated above, or start to use a reverse proxy (nginx for instance), start the process on another port and use the reverse proxy to forward the calls from port 80 to whatever port you started the process on.
The error code EACCES means you don't have proper permissions to run applications on that port. On Linux systems, any port below 1024 requires root access.
you need to use reverse proxy to forward the calls from port 80 to 8080 for example.

ERROR connecting to remote database server(NGINX) via tunneled localhost Node JS

error connecting: Error: Connection lost: The server closed the connection.
at Protocol.end (C:\Users\Tony\Documents\TestServer\node_modules\mysql\lib\protocol\Protocol.js:109:13)
at Socket.<anonymous> (C:\Users\Tony\Documents\TestServer\node_modules\mysql\lib\Connection.js:109:28)
at emitNone (events.js:91:20)
at Socket.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
--------------------
at Protocol._enqueue (C:\Users\Tony\Documents\TestServer\node_modules\mysql\lib\protocol\Protocol.js:141:48)
at Protocol.handshake (C:\Users\Tony\Documents\TestServer\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\Users\Tony\Documents\TestServer\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (C:\Users\Tony\Documents\TestServer\app.js:13:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
using this code
var express = require('express');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
port: '8888',
user: 'root',
password: 'xxx',
database: 'shop'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
server is BITNAMI using google cloud
localhost is on windows, i can connect to the phpmyadmin through the browser but the script does not work. Thank you
Hi Bitnami developer here,
As you mentioned, you can access phpMyAdmin using the 8888 port because you opened a tunnel to access Apache and that application through that port. You would need to open a new tunnel to access MySQL directly because we configure the server to only listen to localhost for security reasons.
The source port of this new tunnel would be a different port (i.e 8989) and the destination would be localhost:3306 (3306 is the MySQL default port). You would need to modify your code to use the 8989 port instead of the 8888 one.
I hope this information helps.
Jota
To find out if the tunnel is really set up you can run.
sudo lsof -i -n | egrep '\<ssh\>'
you should get some lines out of that. If one of them is around these lines :
ssh 11704 root 3u IPv4 47283826 0t0 TCP [localip]:46286->[remoteip]:ssh (ESTABLISHED)
then the connection is on. In this case you should make sure you are connecting your nodejs application to the correct local port. The code above seems correct just make sure that the tunnel is then from port :8888 to the remote database port, maybe :3607 or whatever the port is.
Otherwise, if your tunnel is not set up properly. There are quite a few tutorials out there explaining how to set an ssh tunnel such as this one :
https://support.cloud.engineyard.com/hc/en-us/articles/205408088-Access-Your-Database-Remotely-Through-an-SSH-Tunnel
Make sure that you also use autossh to set up the tunnel. SSH tunnels are unstable and they tend to fall. Autossh will make sure that the connection is brought back up and that is run when the computer boots up.
https://www.everythingcli.org/ssh-tunnelling-for-fun-and-profit-autossh/

How to start node.js on port 80 on a linux server?

When I try to start node on port 80, the error tells me that the port is in use. I imagine that's Apache.
What is the proper way to "take over" port 80, and keep it that way after a server restart?
(Linux xxxx.__.com 2.6.32-5-amd64 #1 SMP Tue Jun 14 09:42:28 UTC 2011 x86_64 GNU/Linux)
you can use ip tables to map port 80 to 8000
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000
to make it permanent
sudo sh -c "iptables-save > /etc/iptables.rules"
and add
pre-up iptables-restore < /etc/iptables.rules
to your /etc/network/interfaces
To take over port 80 when another process is listening on it, you must kill the process (or somehow tell it to stop listening). To ensure that Apache doesn't try to listen on port 80 again the next time it starts, you need to edit its configuration or prevent it from starting up.
To see which process is listening on port 80, run sudo netstat -ntap and look for the row with Local Address ending in port :80. The PID of the process (and the name) is in the far right column.
you can use node.js with node-http-proxy check this link How to use vhosts alongside node-http-proxy? and
How do I run Node.js on port 80?
Thanks & Regards,
Alok
A constantly running unused apache maybe a security hole, in any case no sense in running unused services.
On the chance you're on ubuntu, this what I used..
sudo service apache2 stop
sudo update-rc.d apache2 remove
You can access port 80 once you stop the service currently using it.
In your case, follow these steps:
1) Use systemctl to stop apache2:
sudo systemctl stop apache2
2) Check apache2 status:
sudo systemctl status apache2
Or just by entering http://localhost in your browser. If you get an error, you are good to go.
ERR_CONNECTION_REFUSED
3) Now start your NodeJS server on port 80.
4) You can access your server at http://localhost
UPDATE
If you are seeing errors on your console, try node preceding with sudo
For eg. sudo node server.js
Here are the errors
events.js:137
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object._errnoException (util.js:1003:13)
at _exceptionWithHostPort (util.js:1024:20)
at Server.setupListenHandle [as _listen2] (net.js:1349:19)
at listenInCluster (net.js:1407:12)
at Server.listen (net.js:1495:7)
at Object.<anonymous> (/home/abdus/Desktop/voice-recognition/test.js:7:4)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)

Resources