扔掉//未处理的'错误'事件 - NodeJS

时间:2017-09-03 00:21:58

标签: javascript angularjs node.js gulp nodemon

环境

  • NodeJS

描述

我正在尝试运行我的Node应用程序。

npm run start 

我一直在

> api@1.0.0 start /Users/bheng/Sites/BASE/api
> nodemon ./bin/index.js -w server

[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Users/bheng/Sites/BASE/api/server/**/*
[nodemon] starting `node ./bin/index.js`
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::8000
    at Object.exports._errnoException (util.js:1018:11)
    at exports._exceptionWithHostPort (util.js:1041:20)
    at Server._listen2 (net.js:1258:14)
    at listen (net.js:1294:10)
    at Server.listen (net.js:1390:5)
    at EventEmitter.listen (/Users/bheng/Sites/BASE/api/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/Users/bheng/Sites/BASE/api/bin/index.js:8:5)
    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)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3
[nodemon] app crashed - waiting for file changes before starting...

尝试

的package.json

{
  "name": "api",
  "version": "1.0.0",
  "description": "",
  "main": "bin/index.js",
  "scripts": {
    "start": "./node_modules/.bin/nodemon ./bin/index.js -w server",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bluebird": "^3.5.0",
    "body-parser": "^1.15.2",
    "cors": "^2.8.4",
    "express": "^4.14.0",
    "helmet": "^3.8.1",
    "jwt-decode": "^2.2.0",
    "jwt-express": "^1.1.0",
    "lodash": "^4.17.4",
    "morgan": "^1.7.0",
    "pg": "^6.2.4",
    "pg-hstore": "^2.3.2",
    "request": "^2.81.0",
    "request-promise": "^4.2.1",
    "sequelize": "^4.1.0"
  },
  "devDependencies": {
    "eslint": "^2.13.1",
    "eslint-config-airbnb": "^9.0.1",
    "eslint-plugin-import": "^1.10.2",
    "eslint-plugin-jsx-a11y": "^1.5.5",
    "eslint-plugin-react": "^5.2.2",
    "google-translate-api": "^2.3.0",
    "nodemon": "^1.11.0"
  }
}

./ node_modules /的.bin / nodemon

#!/usr/bin/env node
'use strict';
var cli = require('../lib/cli');
var nodemon = require('../lib/');
var options = cli.parse(process.argv);

nodemon(options);

var fs = require('fs');

// checks for available update and returns an instance
var defaults = require('lodash.defaults');
var pkg = JSON.parse(fs.readFileSync(__dirname + '/../package.json'));

require('update-notifier')({
  pkg: defaults(pkg, { version: '0.0.0' }),
}).notify();

./仓/ index.js

// This will be our application entry. We'll setup our server here.
const http = require('http');
const app = require('../server/app.js'); // The express app we just created
const port = parseInt(process.env.PORT, 10) || 8000;

app.set('port', port);
// const server = http.createServer(app);
app.listen(port, () => {
    console.log(`The server is running at localhost:${port}`);
});

服务器/ app.js

const helmet = require('helmet');
const cors = require('cors');
const express = require('express');
const jwt = require('jwt-express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const Bluebird = require('bluebird');
const lodash = require('lodash');

Promise = Bluebird;
_ = lodash;

const app = express();
app.use(helmet());
app.use(cors());
app.options('*', cors());

app.use(logger('dev'));
app.use(jwt.init('**********', {
    cookies: false
}));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

require('./routes')(app);

app.get('*', (req, res) => res.status(200).send({
  message: 'Welcome to the beginning of nothingness.',
}));

app.use(function(err, req, res, next) {
    if (err.name == 'JWTExpressError') {
        res.status(401).send('401', {
            error: {
                message: err.message
            }
        });
    } else {
        next(err);
    }
});

module.exports = app;

问题

如何继续进行调试?

我现在对任何建议持开放态度。

任何提示/建议/帮助都将非常感谢!

3 个答案:

答案 0 :(得分:2)

就像Abhijeet和Keith指出的那样,在端口8000上还有另一个进程正在运行。

要终止所有NodeJS进程并随后释放8000端口以便在您的应用上使用,请使用以下代码:

killall node

djburdick 指向右here

ps aux | grep node获取NodeJS进程及其ID的列表。

kill -9 PID - 使用使用8000端口的进程ID更改PID。

答案 1 :(得分:1)

  

错误:听EADDRINUSE ::: 8000

您的错误消息刚刚告诉您原因,其他应用程序也使用了8000,尝试在差异端口上运行它。

要获取可安全使用的端口列表,请参阅https://www.browserstack.com/question/664

答案 2 :(得分:1)

  

错误:听EADDRINUSE ::: 8000

  • 找出哪个应用正在使用端口8000:lsof -n | grep LISTEN | grep :8000
  • 第一列将指示流程
  • 要么杀死进程,要么找出需要优雅关闭的应用程序。