MEAN RESTful应用程序错误处理服务器崩溃

时间:2016-08-22 15:50:45

标签: angularjs node.js mongodb express mean-stack

我刚刚使用heroku网页上的MEAN堆栈创建了一个简单的RESTful api教程。我所做的只是克隆包含示例代码的repo,从mLab添加我的mongodb_uri然后在本地运行应用程序(npm start)。

它工作正常,但是当我尝试创建无效条目(不向联系人提供姓名和姓)时,快速服务器崩溃并且在我的数据库上进行输入(这是不一致的)。

我在github repo上打开了一个问题,但我没有得到答案,我认为错误处理一定有问题,但我不知道它可能是什么。

这是我在服务器崩溃时得到的结果:

ERROR: Invalid user input
/Users/nanop/Desktop/mean-contactlist/node_modules/mongodb/lib/utils.js:98
    process.nextTick(function() { throw err; });
                                  ^

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
    at ServerResponse.header (/Users/nanop/Desktop/mean-contactlist/node_modules/express/lib/response.js:719:10)
    at ServerResponse.send (/Users/nanop/Desktop/mean-contactlist/node_modules/express/lib/response.js:164:12)
    at ServerResponse.json (/Users/nanop/Desktop/mean-contactlist/node_modules/express/lib/response.js:250:15)
    at /Users/nanop/Desktop/mean-contactlist/server.js:72:23
    at /Users/nanop/Desktop/mean-contactlist/node_modules/mongodb/lib/collection.js:421:18
    at handleCallback (/Users/nanop/Desktop/mean-contactlist/node_modules/mongodb/lib/utils.js:96:12)
    at /Users/nanop/Desktop/mean-contactlist/node_modules/mongodb/lib/collection.js:726:5
    at /Users/nanop/Desktop/mean-contactlist/node_modules/mongodb-core/lib/connection/pool.js:428:18
    at nextTickCallbackWith0Args (node.js:433:9)

最后这是定义的handleError方法,我认为没关系:

function handleError(res, reason, message, code) {
  console.log("ERROR: " + reason);
  res.status(code || 500).json({"error": message});
}

这是我所指的回购:https://github.com/chrisckchang/mean-contactlist

0 个答案:

没有答案