Node.js应用程序提供ERR_EMPTY_RESPONSE

我在使用Node.js,Express,MongoDB和Mongoose构build应用程序时遇到了严重的问题。 昨天晚上,当我使用nodemon server.js来运行服务器时,一切似乎都奏效了。 在命令行上似乎一切正常,但在浏览器(特别是铬)我得到以下错误:没有收到数据ERR_EMPTY_RESPONSE。 我已经尝试了我的机器上的其他节点项目,他们也努力工作。 我昨晚做了一个npm更新,以便更新我的模块,因为我从MongoDB / Mongoose获得的另一个错误{[错误:找不到模块'../build/Release/bson']代码:'MODULE_NOT_FOUND'}。 我在这个答案中使用的解决scheme来尝试和修复它,它不工作,我仍然得到这个错误。 现在我没有得到任何文件被提供给我的浏览器。 我的代码如下。 请帮忙:

 //grab express and Mongoose var express = require('express'); var mongoose = require('mongoose'); var Schema = mongoose.Schema; //create an express app var app = express(); app.use(express.static('/public/css', {"root": __dirname})); //create a database mongoose.connect('mongodb://localhost/__dirname'); //connect to the data store on the set up the database var db = mongoose.connection; //Create a model which connects to the schema and entries collection in the __dirname database var Entry = mongoose.model("Entry", new Schema({date: 'date', link: 'string'}), "entries"); mongoose.connection.on("open", function() { console.log("mongodb is connected!"); }); //start the server on the port 8080 app.listen(8080); //The routes //The route for getting data for the database app.get("/database", function(req, res) { Entry.find({}, function(err, data) {console.log(err, data, data.length); }); }); //The route for posting data on the database app.post("/database", function(req, res) { //test new post var newMonth = new Entry({date: '1997-10-30', link: 'https://wwww.youtube.com/'}); newMonth.save(function(err) { if (err !== null) { //object was not save console.log(err); } else { console.log("it was saved!") }; }); }); //create an express route for the home page at http://localhost:8080/ app.get('/', function(req, res) { res.send('ok'); res.sendFile('/views/index.html', {"root": __dirname + ''}); }); //Send a message to the console console.log('The server has started'); 

 //The route for getting data for the database app.get("/database", function(req, res) { Entry.find({}, function(err, data) {console.log(err, data, data.length); }); }); //The route for posting data on the database app.post("/database", function(req, res) { //test new post var newMonth = new Entry({date: '1997-10-30', link: 'https://wwww.youtube.com/'}); newMonth.save(function(err) { if (err !== null) { //object was not save console.log(err); } else { console.log("it was saved!") }; }); }); 

这些路由不会通过res发回任何东西给客户端。 bson错误并不是什么大问题 – 它只是告诉你它不能使用C ++ bsonparsing器,而是使用本地JSparsing器。

解决办法可能是:

 //The route for getting data for the database app.get("/database", function(req, res) { Entry.find({}, function(err, data) { if (err) { res.status(404).json({"error":"not found","err":err}); return; } res.json(data); }); }); //The route for posting data on the database app.post("/database", function(req, res) { //test new post var newMonth = new Entry({date: '1997-10-30', link: 'https://wwww.youtube.com/'}); newMonth.save(function(err) { if (err !== null) { res.status(500).json({ error: "save failed", err: err}); return; } else { res.status(201).json(newMonth); }; }); });