How to fix "Error: listen EADDRINUSE: address already in use :::2212" in node js?

Viewed 32

I'm new to node js. I have created a node js on my CentOS7 server, and this is my server.js file with the config of port is 2212:

.
.
.
.

app.get('*', function(req, res) {
    res.redirect('https://visitor.maharaja.id'); 
});

const db = mysql.createConnection({
  host: '192.168.200.121',
  user: 'root',
  password: 'rahasia',
  database: 'wav4'
});
 
db.connect((err) =>{
  if(err) throw err;
  console.log('Mysql Connected...');
});

const configs = {
    port: 2212, // custom port to access server
    url_callback : 'https://visitor.maharaja.id/WASenderPro/helper/callback.php'
};

cron.schedule('5 * * * *',  function() {
  console.log('cronjob berjalan')
  const savedSessions = getSessionsFile();

.
.
.
.

server.listen(configs.port, function () {
    console.log('App running on *: ' + configs.port);
});

And when i run those code with the command "node server.js", i got this error message:

node:events:505
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::2212
    at Server.setupListenHandle [as _listen2] (node:net:1372:16)
    at listenInCluster (node:net:1420:12)
    at Server.listen (node:net:1508:7)
    at Object.<anonymous> (/var/www/html/WASenderPro/server.js:478:8)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1399:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 2212
}

But, when I change the port to 8181 or another port, i got this different error messages:

App running on *: 8181
Mysql Connected...
node:events:505
      throw er; // Unhandled 'error' event
      ^

Error: Connection lost: The server closed the connection.
    at Protocol.end (/var/www/html/WASenderPro/node_modules/mysql/lib/protocol/Protocol.js:112:13)
    at Socket.<anonymous> (/var/www/html/WASenderPro/node_modules/mysql/lib/Connection.js:94:28)
    at Socket.<anonymous> (/var/www/html/WASenderPro/node_modules/mysql/lib/Connection.js:526:10)
    at Socket.emit (node:events:539:35)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted 'error' event on Connection instance at:
    at Connection._handleProtocolError (/var/www/html/WASenderPro/node_modules/mysql/lib/Connection.js:423:8)
    at Protocol.emit (node:events:527:28)
    at Protocol._delegateError (/var/www/html/WASenderPro/node_modules/mysql/lib/protocol/Protocol.js:398:10)
    at Protocol.end (/var/www/html/WASenderPro/node_modules/mysql/lib/protocol/Protocol.js:116:8)
    at Socket.<anonymous> (/var/www/html/WASenderPro/node_modules/mysql/lib/Connection.js:94:28)
    [... lines matching original stack trace ...]
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  fatal: true,
  code: 'PROTOCOL_CONNECTION_LOST'
}

Thanks in advance for anyone who can help me.

1 Answers

The first error says address port is in use. This is probably due to same process still running. Or another process using same port 2212.

The second error is probably about connecting to your MySQL. From this popular answer might be solution. Don't forget to restart services. Maybe a firewall preventing connection to MySQL, did you allow remote connections? try thru localhost.

Related