Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Very basic Node.js app using MongoLab and Heroku
node_modules
{
"name": "mongolabtest",
"version": "1.0.0",
"description": "Sample app using Heroku, Node.js, and MongoLab",
"engines":{
"node": "0.6.x",
"npm": "1.0.x"
},
"dependencies": {
"connect": "1.8.x",
"mongodb": "0.9.x"
}
}
web: node server.js
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
});
});
});
Owner

Updated to include "engines" section in package.json. This forces heroku to use the latest version of node and npm so that the latest version of the mongodb package can be properly loaded.

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