是什么原因导致我的终端出现此错误?

时间:2019-01-23 12:34:44

标签: javascript mysql node.js

这是我的错误,我尝试使用另一个端口号,但相同的错误不断出现。但是,尽管终端继续显示以下错误,但页面工作正常:

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)

这是我在终端上加载的nodejs代码:

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

此错误的原因是什么?

2 个答案:

答案 0 :(得分:0)

打开CMD并执行以下操作

  1. 通过在CMD“ netstat -ano | findstr:3000”中运行命令来获取过程PID。

  2. 并杀死该进程“ taskkill / PID 8664 / F”

在我的情况下,pid为8664

答案 1 :(得分:0)

获取正在运行的进程并杀死它。

sudo lsof -i :3000
kill $PID