Search code examples
javascriptnode.jsmongodbnpm

Node.js app giving ERR_EMPTY_RESPONSE


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');

Solution

  • //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);
            };
        });
    });