Node.js应用程序提供ERR

编程入门 行业动态 更新时间:2024-10-19 08:54:57
本文介绍了Node.js应用程序提供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: 'wwww.youtube/'}); 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 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: 'wwww.youtube/'}); 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: 'wwww.youtube/'}); 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

本文发布于:2023-10-11 23:04:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483089.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   Node   js   ERR

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!