Skip to content

Instantly share code, notes, and snippets.

@KameronKales
Created June 29, 2016 21:41
Show Gist options
  • Save KameronKales/846ec9246a799c3661c35e9e36296d2c to your computer and use it in GitHub Desktop.
Save KameronKales/846ec9246a799c3661c35e9e36296d2c to your computer and use it in GitHub Desktop.
fixed server.js from meows
var express = require ('express');
var MongoClient = require ('mongodb').MongoClient;
var bodyParser = require ('body-parser');
var app = express ();
var db = null;
MongoClient.connect("mongodb://localhost:27017/mittens", function(err,dbconn) {
if (!err) {
console.log("we are connected bro!");
db = dbconn;
}
});
app.use(bodyParser.json());
app.use(express.static('public'));
app.get('/meows', function (req, res, next) {
db.collection('meows', function(err, meowsCollection) {
meowsCollection.find().toArray(function(err, meows) {
console.log(meows);
return res.json(meows);
});
});
});
app.post('/meows', function(req, res, next) {
var meowsCollection = db.collection('meows');
var newMeow = {
text: req.body.newMeow
};
meowsCollection.insertMany([newMeow], function(err, result) {
if (err) {
console.log("Error", err);
return res.status(500).send("Error" + err);
}
console.log("Success", result);
res.send(result);
});
});
app.listen(3000, function() {
console.log('example app listening on port 3000!');
});
@KameronKales
Copy link
Author

Would be great if you could explain why it didn't work the previous way. I am a bit lost on that

@DaveDevTV
Copy link

Which lines did you change to get it working?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment