-1

This is my error and I have tried using another port number but the same error keep coming out. However, the page works fine, though the terminal keep showing the error below:

events.js:167
  throw er; // Unhandled 'error' event
  ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1290:14)
    at listenInCluster (net.js:1338:12)
    at Server.listen (net.js:1425:7)
    at Object.<anonymous> (/Users/jade/Desktop/node.js-mysql-1/main.js:160:5)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
Emitted 'error' event at:
    at emitErrorNT (net.js:1317:8)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

This is my nodejs code that I load on the terminal:

var http = require('http');
var fs = require('fs');
var url = require('url');
var qs = require('querystring');
var template = require('./lib/template.js');
var path = require('path');
var sanitizeHtml = require('sanitize-html');
var mysql = require('mysql');
var db = mysql.createConnection({
  host:'localhost',
  user:'nodejs',
  password:'111111',
  database:'opentutorials'
});
db.connect();

var app = http.createServer(function(request,response){
    var _url = request.url;
    var queryData = url.parse(_url, true).query;
    var pathname = url.parse(_url, true).pathname;
    if(pathname === '/'){
      if(queryData.id === undefined){
        db.query(`SELECT * FROM topic`, function(error,topics){
          var title = 'Welcome';
          var description = 'Hello, Node.js';
          var list = template.list(topics);
          var html = template.HTML(title, list,
            `<h2>${title}</h2>${description}`,
            `<a href="/create">create</a>`
          );
          response.writeHead(200);
          response.end(html);
        });
ellipsis...

app.listen(3000);

What is the cause of this error?

Marwan Ansari
  • 123
  • 1
  • 10
zoe
  • 1
  • 1
  • Try app.listen(5000) first line of error says port 3000 is already in use – SanSolo Jan 23 '19 at 12:37
  • 1
    You already have a server running on port 3000, which is why the page works fine. You need to kill the running server. –  Jan 23 '19 at 12:37
  • port 5000 also causes same error and no matter the number is the same error comes out. how can i kill the running server? – zoe Jan 23 '19 at 13:04

2 Answers2

0

Open CMD and Do The Following

  1. Get The Process PID by running command in CMD "netstat -ano | findstr :3000"

  2. And kill that process "taskkill /PID 8664 /F"

in my case pid was 8664

Tayyab Mehar
  • 321
  • 3
  • 7
  • i am a mac user so i tried netstat -vanp tcp | grep 3000 and then sudo lsof -i tcp:3000 but it doesn't worked. and i don't know what is pid :( can you explain what pid is? – zoe Jan 23 '19 at 13:17
  • Its a process id running on the that port – Tayyab Mehar Jan 23 '19 at 13:20
  • Find: "sudo lsof -i :3000" and to kill "kill -9 " check it here https://stackoverflow.com/questions/3855127/find-and-kill-process-locking-port-3000-on-mac – Tayyab Mehar Jan 23 '19 at 13:22
0

Get running process and kill it.

sudo lsof -i :3000
kill $PID
Xcrowzz
  • 182
  • 1
  • 19