Node.js应用程序提供ERR_EMPTY_RESPONSE [英] Node.js app giving ERR_EMPTY_RESPONSE
问题描述
我使用Node.js,Express,MongoDB和Mongoose构建的应用程序存在严重问题.昨晚,当我使用nodemon server.js
来运行服务器时,一切似乎都可以正常工作.在命令行上,所有内容似乎都可以正常运行,但是在浏览器(特别是Chrome)上,我收到以下错误消息:没有收到数据ERR_EMPTY_RESPONSE.我已经在计算机上尝试了其他Node项目,但它们也都在努力工作.昨晚我做了一次npm更新,以更新我的模块,因为我从MongoDB/Mongoose {[错误:找不到模块'../build/Release/bson']代码:'MODULE_NOT_FOUND'}中收到另一个错误.我在此 answer 中使用了该解决方案来尝试对其进行修复,但并没有不能正常工作,但我仍然收到该错误.现在,我根本没有将任何文件提供给浏览器.我的代码如下.请帮助:
I'm having serious issues with an app I am building with Node.js, Express, MongoDB and Mongoose. Last night everything seemed to work when I used nodemon server.js
to `run the server. On the command line everything seems to be working but on the browser (in particular Chrome) I get the following error: No data received ERR_EMPTY_RESPONSE. I've tried other Node projects on my machine and they too are struggling to work. I did a npm update last night in order to update my modules because of another error I was getting from MongoDB/Mongoose { [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND'}. I used the solution in this answer to try and fix it and it didn't work and I still get that error. Now I don't get any files at all being served to my browser. My code is below. Please help:
//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 ++ bson解析器,而是使用本机JS.
These routes don't send anything back to the client via res
. The bson error isn't a big deal - it's just telling you it can't use the C++ bson parser and instead is using the native JS one.
修复可能是:
//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);
};
});
});
这篇关于Node.js应用程序提供ERR_EMPTY_RESPONSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!