application times out when connecting to MongoLab from Heroku - node.js

I am hosting a node.js application on Heroku and trying to connect to MongoLab using the node module node-mongodb-native to connect. My application works fine when run from localhost connecting to MongoLab, but after deploying to Heroku I get an Application Error H12 (Request timeout).
Sample code:
app.get('/', function(req, res) {
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('mycollection', function(err, coll){
coll.find().toArray(function(error, results) {
if(error) console.log(error)
else {
res.send(util.inspect(results));
}
});
});
});
});
Are there additional options I need to pass to .connect() from Heroku?
Any suggestions are greatly appreciated. Thanks!

In case anyone else has this issue:
It is now possible to choose what version of node you would like to run on Heroku. So by adding the following code to my package.json I was able to connect to MongoLab no problem:
"engines": {
"node": "0.6.12"
, "npm": "1.1.4"
}
Thanks.

Related

Unable to run node API on Ubuntu (AWS)

I have deployed node API on Ubuntu server (AWS) but when I run my app it throw below error given in screen shot. due to I am not able to make API requst.
I have trid to kill port and execute it again but it gives e same error
my node app is running on 8081 on local machine
var connection = mongoose
.connect(url, option)
.then(result => {
app.listen(PORT);
console.log("Running RestHub on port " + PORT)
})
.catch(err => {
console.log(err);
});
after uploading code in Ubuntu sever it does not work
I am struggling to fix this issue since yesterday but could not fix yet.
Please your help would be highly appreciated
Thanks you

How to publish a API project with heroku?

I try to publish an API application with Heroku but I am getting an error. What could be the reason? I might be doing something missing. Can you show me what I'm missing?
heroku logs --tail Result
Application index
edit:
I dont have your code snippet so i'm just giving my example here for local development and heroku setup both
In local development
install dotenv npm package
create .env file in root directory and set MONGOURI='Your mongo string here'
then you can import dotenv package in root as shown in below example and use variable as process.env.MONGOURI
On Heroku
there are multiple ways you can do same thing as above which are listed here in config vars. More specifically i'll suggest you to do this way from heroku frontend
require('dotenv').config(); // <-- install `dotenv` package
const MongoClient = require('mongodb').MongoClient;
const mongoUrl = process.env.MONGOURI;
// Connect to the db
MongoClient.connect(mongoUrl, function (err, db) {
if(err) throw err;
db.open(function(err, mongoClient) {
console.log("mongo connected");
});
db.close();
});
I had created a node with mongo boilerplate project to get me started you can check if you want node-auth-mongo

connecting node js to postgresql on heroku

I have deployed my app on heroku and was able to make a connection to the database from pg:psql in my terminal by copying and pasting the code provided by heroku. Now i want to directly connect my node app to postgres on heroku but no success. I followed the instruction on heroku on how to connect nodejs to postgres here.. https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js but i can't seem to get it to work. I simply installed pg module and pasted the block of code provided by heroku inside my get request in my server.js file.
app.use(express.static('public'));
app.use(bodyParser.json());
app.get('/food', function(req, res) {
pg.defaults.ssl = true;
pg.connect(process.env.DATABASE_URL, function(err, client) {
if (err) throw err;
console.log('Connected to postgres! Getting schemas...');
client.query('SELECT table_schema,table_name FROM information_schema.tables;').on('row', function(row) {
console.log(JSON.stringify(row));
});
});
});
http.listen(process.env.PORT || 3000, function(){
console.log('listening to port 3000!');
});
the error i get from heroku logs is..
/appserver.js:53
if (err) throw err;
Error: connect ECONNREFUSED 127.0.0.0.1:5432
after searching for that error message, i get some people suggesting on fixing or changing the DATABASE_URL. How and where do i change it? I'm lost. I would really appreciated if i can get some help to fix it.
The pg connection is using the database url in your environment:
pg.connect(process.env.DATABASE_URL, function(err, client) {}
Because the DATABASE_URL env variable is not set, it defaults to localhost 127.0.0.0.1:5432. You can set DATABASE_URL in your bashrc or bash_profile. You can name it more specifically PROJECTNAME_DATABASE_URL if you have multiple apps. On Heroku, you can set it like this:
$ heroku config:set DATABASE_URL=postgres://user:password#host:port/database

Node.js Heroku app crashes with Error R10 (Boot timeout) `heroku restart` not working

Using the below code from an example I found, my Heroku Node.js app crashes. It is not responding when I navigate to [myapp].herokuapp.com
var http = require('http');
var port = process.env.PORT || 8000;
var counter = 0;
http.createServer(function (req, res) {
// increment the counter for each visitor request
counter=counter+1;
var path = req.url;
console.log("requested=" + path + " counter=" + counter);
res.writeHead(200, {'Content-Type': 'text/html'}); // prepare response headers
res.end(" :) ");
}).listen(port);
my procfile is
web: node thenameofmyapp.js
I have done heroku ps:scale web=1
I had some dependencies but removed them to try to get a simple app to work. My current package.json is
{
"name": "thenameofmyapp",
"version": "0.0.3",
"dependencies": {
},
"engines": {
"node": "0.10.x",
"npm": "1.2.x"
}
}
Why can't my app bind to the port?
EDIT: It looks like even after multiple updates and running heroku restart, my app is still running code that doesn't exist anymore. What's going on here?
I'm not completely sure what happened, but it looked like Heroku was not restarting when I pushed new code or ran heroku restart. I created a new app with the same code and it works.
One difference someone else might want to test is that I removed node_packages from my git repo before pushing to the new app.
You can also check what is happening with your app by doing: heroku logs (you have to install the herokucli before).
This error is most likely caused by a process that is not able to reach an external resource or it has to many code dependencies, or there's to many evaluations, parsing during start.

Heroku + mongoose connection error: no primary server found in set

I have a mongodb replica set on mongolab.
I'm using nodejs + mongoose. When I'm trying to connect from my local machine everything goes ok.
But after deployment to heroku something wrong happens and mongoose got strange error:
[Error: no primary server found in set]
Here some code (server.js):
async.series([
function(callback){
console.log('DB Connection: ' + siteConf.mongo_url);
mongoose.connect(siteConf.mongo_url, siteConf.mongo_options, callback);
},
function(callback){
http.createServer(app).listen(siteConf.port, callback);
}
],
function(err, results){
if (err) {
console.log(err);
}
console.log('Running in ' + (process.env.NODE_ENV || 'development') + ' mode # ' + siteConf.uri);
}
);
This url I'm using as connection string:
mongodb://username:password#someid-a0.mongolab.com:39897/pm_prod,mongodb://someid-a1.mongolab.com:39897
The main thing I can't understand is: what is the differences between my maching and heroku cloud hosting.
I already tried to remove node_modules and npm install them to be sure that I have same versions as on heroku. (Because heroku do this on each deploy).
Thanks, and sorry for my bad english
This may be a URI problem. The format for DB URIs is:
mongodb://<user>:<pass>#host:port,host:port,...,host:port/db_name
That means that
mongodb://username:password#someid-a0.mongolab.com:39897/pm_prod,mongodb://someid-a1.mongolab.com:39897
should be:
mongodb://username:password#someid-a0.mongolab.com:39897,someid-a1.mongolab.com:39897/pm_prod
It's also worth noting for others: If you are using Heroku's MongoLab add-on, your URI is available as an environment variable at process.env.MONGOLAB_URI, so you don't have to place the URI in your code.
This could also be related to a running question about connectivity between Heroku and Mongo. See https://github.com/jcottr/temps-mort, which references two tickets for the Node Mongo driver:
https://jira.mongodb.org/browse/NODE-4
https://jira.mongodb.org/browse/NODE-5

Resources