-
-
Save brianyang/1450064 to your computer and use it in GitHub Desktop.
Very basic Node.js app using MongoLab and Heroku
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
node_modules |
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
{ | |
"name": "mongolabtest", | |
"version": "1.0.0", | |
"description": "Sample app using Heroku, Node.js, and MongoLab", | |
"dependencies": { | |
"connect": "1.8.x", | |
"mongodb": "0.9.x" | |
} | |
} |
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
web: node server.js |
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 connect = require('connect'), | |
mongo = require('mongodb'); | |
// Connect to a mongo database via URI | |
// With the MongoLab addon the MONGOLAB_URI config variable is added to your | |
// Heroku environment. It can be accessed as process.env.MONGOLAB_URI | |
mongo.connect(process.env.MONGOLAB_URI, {}, function(error, db){ | |
// console.log will write to the heroku log which can be accessed via the | |
// command line as "heroku logs" | |
db.addListener("error", function(error){ | |
console.log("Error connecting to MongoLab"); | |
}); | |
db.createCollection('requests', function(err, collection){ | |
db.collection('requests', function(err, collection){ | |
var requestCollection = collection; | |
connect( | |
connect.favicon(), // Return generic favicon | |
connect.query(), // populate req.query with query parameters | |
connect.bodyParser(), // Get JSON data from body | |
function(req, res, next){ // Handle the request | |
res.setHeader("Content-Type", "application/json"); | |
if(req.query != null) { | |
requestCollection.insert(req.query, function(error, result){ | |
// result will have the object written to the db so let's just | |
// write it back out to the browser | |
res.write(JSON.stringify(result)); | |
}); | |
} | |
res.end(); | |
} | |
).listen(process.env.PORT || 8080); | |
// the PORT variable will be assigned by Heroku | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment