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 update 以更新我的模块,因为我从 MongoDB/Mongoose { [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' 得到另一个错误}.我使用此 answer 中的解决方案来尝试修复它,但它没有用,我仍然收到该错误。现在我根本没有将任何文件提供给我的浏览器。我的代码如下。请帮助:
//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 解析器。
修复可能是:
//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);
};
});
});
2020 年 6 月更新
ERR_EMPTY_RESPONSE 表达js
package.json
"cors": "^2.8.4",
"csurf": "^1.9.0",
"express": "^4.15.4",
当您尝试使用错误的 HTTP 请求访问时会显示此错误。首先检查您的请求是否正确
也许你的 cors 参数错误
我使用 Node.js、Express、MongoDB 和 Mongoose 构建的应用程序出现严重问题。昨晚当我使用 nodemon server.js
到 `运行 服务器时,一切似乎都正常。在命令行上,一切似乎都在工作,但在浏览器上(特别是 Chrome),我收到以下错误:未收到数据 ERR_EMPTY_RESPONSE。我在我的机器上尝试过其他 Node 项目,它们也很难工作。我昨晚做了一个 npm update 以更新我的模块,因为我从 MongoDB/Mongoose { [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' 得到另一个错误}.我使用此 answer 中的解决方案来尝试修复它,但它没有用,我仍然收到该错误。现在我根本没有将任何文件提供给我的浏览器。我的代码如下。请帮助:
//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 解析器。
修复可能是:
//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);
};
});
});
2020 年 6 月更新
ERR_EMPTY_RESPONSE 表达js
package.json
"cors": "^2.8.4",
"csurf": "^1.9.0",
"express": "^4.15.4",
当您尝试使用错误的 HTTP 请求访问时会显示此错误。首先检查您的请求是否正确
也许你的 cors 参数错误