【问题标题】:node events.js:136 throw er; Unhandled 'error' event节点 events.js:136 throw er;未处理的“错误”事件
【发布时间】:2018-05-04 02:19:18
【问题描述】:

我得到 events.js:136 throw er;未处理的“错误”事件不知道如何以及为什么? 我也重新安装了 nodejs 和 mongodb 但抛出了同样的错误。

package.json

{
"name": "bookstore",
"version": "1.0.0",
"description": "simple bookstore app",
"main": "app.js",
"devDependencies": {},
"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
    "express": "*",
    "body-parser": "*",
    "mongoose": "*"
},
"author": "AveesP",
"license": "ISC"

}

App.js 代码

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
//connect to mongoose
mongoose.connect('mongodb://localhost/bookstore');
var db = mongoose.connection;
app.get('/', function(req, res) {
   res.send('Hello world!');
});
app.listen(3000);
console.log('Running on port 3000...');

【问题讨论】:

  • 您对var bodyParser = require(); 有什么期望?你认为var bodyParser = require('body-parser') 会更好吗?
  • 我已经更新了显示“events.js:136 throw er; Unhandled 'error' event”的问题
  • require('') 也好不到哪里去。您需要传递 require() 一个值。
  • 这是我写的 var bodyParser = require('body-parser');
  • 好的,现在你得到了一个不同的错误。你确定 Mongo 正在运行吗?

标签: node.js mongodb express npm


【解决方案1】:

尝试使用以下命令来扩大激活观察者(应用程序更改的监听器)的处理程序的数量

sudo sysctl fs.inotify.max_user_watches=524288 sudo sysctl -p --system

【讨论】:

  • 运行 "docpress s" 时出现此错误,可能是 docpress 无法观看这么多文件
  • 谢谢@andhdo。这个对我有用。为什么我必须使用这些命令?
【解决方案2】:

我已经找到了根本原因..我没有使用 mongodb 创建任何数据库,它是一个连接错误..

感谢 Mark 抽出宝贵时间。

【讨论】:

    【解决方案3】:

    端口问题

    app.listen(8080);
    

    试试这些代码

      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(); //end the response
    }).listen(8080); //the server object listens on port 8080
    

    【讨论】:

      猜你喜欢
      • 2019-08-21
      • 2018-11-19
      • 2020-12-12
      • 1970-01-01
      • 2019-11-10
      • 2018-07-23
      • 2015-03-01
      • 2019-01-27
      • 2017-06-23
      相关资源
      最近更新 更多