7

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');
Community
  • 1
  • 1
Siya
  • 829
  • 4
  • 17
  • 29

2 Answers2

5
//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!")
        };
    });
});

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.

A fix could be:

//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);
        };
    });
});
NG.
  • 22,560
  • 5
  • 55
  • 61
  • I feel like such an idiot. I deleted my all time history. Did a virus scan for most of the day. Thanks man. This helped a lot. – Siya Jun 18 '15 at 14:22
  • Could you also say why Node/Express is not serving the static files (in the public folder) in the project. – Siya Jun 18 '15 at 14:37
  • Maybe `app.use('/public/css', express.static(__dirname));` is what you want - I am not familiar with the way you called it. – NG. Jun 18 '15 at 14:56
-1

updated june 2020

ERR_EMPTY_RESPONSE express js

package.json

 "cors": "^2.8.4",
    "csurf": "^1.9.0",
    "express": "^4.15.4",

this error show when you try to access with the wrong HTTP request. check first your request was correct
maybe your cors parameter wrong

Mr Coder
  • 507
  • 3
  • 13