I am now using Node.js in a Heroku App with a MondoDB in MongoLab service. I put in a collection in localhost DB and on MongoLab the nextdocument.
{
"person": "Jon Smith",
"age": 57
}
And then I use the next script for get the document.
var express = require('express');
var app = express();
var mongojs = require('mongojs');
var databaseUrl = process.env.MONGOLAB_URI || 'mongodb://localhost/person';
var collections = ['person'];
var db = mongojs(databaseUrl, collections);
app.set('port', (process.env.PORT || 3000));
app.use(express.static(__dirname + '/public'));
app.get('/person', function (request, response) {
db.person.find(function (err, docs) {
console.log(docs);
response.json(docs);
});
});
app.listen(app.get('port'), function () {
console.log('[SERVER]', new Date(), 'Start');
});
When access http://localhost:3000/person I get the Json the inserted document, but when access http://my-heroku-app.herokuapp.com/person not. I already tested the environment variable and check the config datas.
Let's consider how these being the MangoLab data:
- Database: heroku_database
- Collection: person
- User: heroku_user
And the environment variable, something like this:
- MONGOLAB_URI - mongodb://heroku_user:password@subdomain.mongolab.com:port/heroku_database
This is my log in Heroku:
2015-10-08T06:35:45.728792+00:00 app[web.1]: > my-app@0.0.1 start /app
2015-10-08T06:35:45.728794+00:00 app[web.1]: > node server.js
2015-10-08T06:35:45.728795+00:00 app[web.1]:
2015-10-08T06:35:46.105279+00:00 app[web.1]: [SERVER] Thu Oct 08 2015 06:35:46 GMT+0000 (UTC) Start
2015-10-08T06:35:46.682857+00:00 heroku[web.1]: State changed from starting to up
2015-10-08T06:35:46.697341+00:00 heroku[web.1]: Process exited with status 143
2015-10-08T06:36:05.210688+00:00 heroku[router]: at=info method=GET path="/" my-heroku-app.herokuapp.com request_id="blah-blah-blah" dyno=web.1 connect=1ms service=17ms status=200 bytes=918
2015-10-08T06:36:05.512399+00:00 heroku[router]: at=info method=GET path="/controllers/controller.js" host=my-heroku-app.herokuapp.com request_id="blah-blah-blah" dyno=web.1 connect=1ms service=4ms status=200 bytes=503
2015-10-08T06:36:07.828198+00:00 heroku[router]: at=info method=GET path="/person" host=my-heroku-app.herokuapp.com request_id="blah-blah-blah" fwd="ip.ip.ip.ip" dyno=web.1 connect=1ms service=31ms status=200 bytes=178
2015-10-08T06:36:07.822645+00:00 app[web.1]: undefined