Created
June 29, 2016 21:41
-
-
Save KameronKales/846ec9246a799c3661c35e9e36296d2c to your computer and use it in GitHub Desktop.
fixed server.js from meows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Which lines did you change to get it working?