I'm getting an unhandled error when trying to run node server - node.js

So, I'm really new to node and I feel like this might be something very simple but I can't figure it out. I installed the dependencies but when I try to start the server by doing "node server" I get this error:
Vladimirs-MBP:comeat-server Vladimir$ node server
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL 18.194.133.202:3001
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at Server.setupListenHandle [as _listen2] (net.js:1350:19)
at listenInCluster (net.js:1408:12)
at GetAddrInfoReqWrap.doListen [as callback] (net.js:1517:7)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:97:10)

Your 3001 port is already used by some service, please change the port
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end();
}).listen(8080); //the server object listens on port 8080
You can use these commands to check what ports are being used:
1. sudo lsof -i -P -n | grep LISTEN
2. netstat -tulpn | grep LISTEN
You can kill that process number using
sudo kill -9 process_number

Your 3001 port is in use. you should use another port or kill 3001 port and then use it.
for example
sudo kill -9 $(sudo lsof -t -i:3001)

Related

PORT 3306 Already In use when i rn nodemon in NodeJS app

I have a nodejs app which was running just fine all this time until I opened MySQL Workbench and also included .env file in my project. I was in the process of deploying the system to Digital Ocean managed database service when I opened MySQL Workbench to visualise the process and not use the mysql shell. Everything worked fine and I migrated my db on to the DO database cluster.
I also wanted to make my app more secure, so I bumped into the .env file method and tried my best to follow through and I came up with this:
Step 1:
npm i dotenv --save
Step 2: Added require('dotenv').config() to my server.js file
Step 3: Update my DB connection file
const mysql = require("mysql");
const conn = mysql.createConnection({
host: process.env.HOST,
user: process.env.USERNAME,
password: process.env.PASSWORD,
//rsport : (process.env.PORT),
database: process.env.DATABASE_NAME,
multipleStatements: true,
});
conn.connect((err) => {
if (err) {
console.log("Oops!, Failed to connect to the database.");
} else {
console.log("Database connection succesfull!");
}
});
module.exports = conn;
Step 4: I set my local .env file and remote .env file accordingly
Step 5: I run nodemon the it return the following error:
body-parser deprecated undefined extended: provide extended option server.js:27:17
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3306
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at Server.listen (net.js:1452:7)
at Function.listen (G:\Maxiko Payment System\Systems\Management Apps\Microservices\mx-core\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (G:\Maxiko Payment System\Systems\Management Apps\Microservices\mx-core\server.js:47:5)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1345:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 3306
}
[nodemon] app crashed - waiting for file changes before starting...
What am I doing wrong? honestly I do not think .env has anything to do with this, but i definately know MySQL Workbench made its own connection. So am I to believe that only one application can connect to a database at one time? That doesnt sound right to me either.
Try running your node js server on a different port or run this command in command prompt (as administrator)
netstat -ano | findstr :<PORT>
Replace with your port number that is in use(in your case 3306)
Then it will show you the PID of your process
Something like this
TCP 0.0.0.0:3306 0.0.0.0:0 LISTEN 77777
77777 is the PID
then run this command
taskkill /PID <PID> /F
Replace with the PID you got in the last command
Its look like some other process already running on your port 3306
You can check it by using below command
lsof -i tcp:3306
It will list you process that is already on the port 3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 12012 user 20u IPv6 86535 0t0 TCP *:3306 (LISTEN)
That shows a process with PID 12012 already using your port 3306
If this process is redundant you can kill it by following command:
sudo kill -9 PID
Replace PID with your process id which in my case is 12012
otherwise you can use some other port to run your node server
I have found the problem. There was indeed another process running and using the same port. In app server initialization script , I was setting the port like this:
app.use('port', process.env.PORT || 5000)
Where at the the fdatabase connection file, I set the ports also as
const conn = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
port : (process.env.DB_PORT),
database: process.env.DB_NAME,
multipleStatements: true,
});
So, as you can see, there is supposed to be a port conflict since i only had on PORT=3006 variable in the .env file.
SOLUTION
All I had to do was specify the database port as
DB_PORT = 3006
APP_PORT = 5000
in the environment variables.
Thank you and Goog luck.

Port specified when running node is undefined

The port is specified when i ran the node project says undefined.
> sf-chain#1.0.0 dev E:\System\dev\node\sf-chain
> nodemon ./app "HTTP_PORT=3002" "P2P_PORT=5002" "PEERS=ws://localhost:5001"
[nodemon] 1.18.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./app HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001`
process.env.HTTP_PORT :: undefined
HTTP_PORT :: 3001
Listening for peer-to-peer connections on: 5001
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3001
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1259:14)
at listen (net.js:1295:10)
at Server.listen (net.js:1391:5)
at EventEmitter.listen (E:\System\dev\node\sf-chain\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (E:\System\dev\node\sf-chain\app\index.js:28:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
[nodemon] app crashed - waiting for file changes before starting...
I ran the first instance using
npm run dev
const HTTP_PORT = process.env.HTTP_PORT || 3001;
The application starts successfully running on port 3001 since i didn't specify HTTP_PORT at the run command.
But when trying to run another instance by specifying HTTP_PORT using the below command
npm run dev HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001
I get this error.
Error: listen EADDRINUSE :::3001
Which means the HTTP_PORT specified at run time is seen as undefined that's why it's trying to using the 3001 port for the first instance.
Error: listen EADDRINUSE
- error comes when somthing is already running/listen on that port
Its because sometime instance of same nodejs acquire port and didnt kill/exit the process properly,
if you have Ubuntu check first which process is using port by
lsof -i :3001 // Port no.
then output will be some thing like this.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 2496 nasiruddin 13u IPv6 37696 0t0 TCP *:3001 (LISTEN)
kill using PID
kill 2496
then start again

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.

nodejs error EADDRINUSE

I get this when i type "node site.js" im 100% sure im not running this node twice.
Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1262:14)
at listen (net.js:1298:10)
at Server.listen (net.js:1376:9)
at Server.listen.Server.attach (/root/node_modules/socket.io/lib/index.js:228:9)
at Timeout._onTimeout (/root/nodejs/site.js:1005:29)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 3000 }
Im using FEDORA 23
Your site.js tries to listen twice on that port or something (another process) is already listening on port 3000. Find the service and stop/kill it. This command should help: lsof -i | grep 3000
Try running killall node, then node site.js again. If that doesn't work, at least you can rule out Node from the cause of this.
Your site is listening twice to that port. You can check in your code whether you have 2 instructions .listen()

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