0

I am following this tutorial to host a Rest API at my openshift account:

Day 27: Restify–Build Correct REST Web Services in Node.js

As per my requirements I made few changes, below is the code:

server.js

var restify = require('restify');
var mongojs = require('mongojs');

var ip_addr = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var port    =  process.env.OPENSHIFT_NODEJS_PORT || 8080;

var server = restify.createServer({name: 'bemeta'});
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(restify.CORS());

server.listen(port, ip_addr, function(){
              console.log(server.name + ' listening at url ' + server.url);
              });

var connectionString = '127.0.0.1:27017/bemeta';

if(process.env.OPENSHIFT_MONGODB_DB_PASSWORD){
    connectionString = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ':' +
    process.env.OPENSHIFT_MONGODB_DB_PASSWORD + '@' +
    process.env.OPENSHIFT_MONGODB_DB_HOST + ':' +
    process.env.OPENSHIFT_MONGODB_DB_PORT + '/' +
    process.env.OPENSHIFT_APP_NAME;
}

var db = mongojs(connectionString, ['bemeta']);
var beRecords = db.collection("be_records");

var PATH = '/be_records';

// get all records
server.get({path: PATH, version:'0.0.1'}, findAllRecords);

// create new record
server.post({path: PATH, version:'0.0.1'}, createRecord);

// defining call backs
function findAllRecords(req, res, next){
    res.setHeader('Access-Control-Allow-Origin', '*');
    beRecords.find(function(err, docs){
                   console.log('Response docs ' + docs);
                   console.log('Error '+ err);

                   if(docs){
                   res.send(200, docs);
                   return next();
                   }

                   return next(err);
                   });
}


function createRecord(req, res, next){
    res.setHeader('Access-Control-Allow-Origin', '*');

    var beRecord = {};
    beRecord.be_id = req.params.be_id;
    beRecord.item_title = req.params.item_title;
    beRecord.item_description = req.params.item_description;
    beRecord.item_link = req.params.item_link;

    beRecords.save(beRecord, function(err, docs){
                   console.log('Response docs ' + docs);
                   console.log('Error '+ err);
                   if(docs){
                   res.send(201, beRecord);
                   return next();
                   }
                   else {
                   next(err);
                   }
                   });
}

When I run it at local and fire a get request via postman by typing below url:

http://127.0.0.1:8080/be_records

I get response as: [], which is correct since I have not inserted any document in respective collection.

But when I run it at my openshift account and fire a get request via postman by typing below url:

http://bemeta-{domain-name}.rhcloud.com/be_records

I get response as:

{
  "code": "InternalError",
  "message": "auth fails"
}

In my rhc console I see below messages:

==> app-root/logs/mongodb.log <==
Wed Feb 24 01:32:23.543 [conn3]  authenticate db: bemeta { authenticate: 1, user: "admin", nonce: "some nonce", key: "some key" }
Wed Feb 24 01:32:23.544 [conn3] auth: couldn't find user admin@bemeta, bemeta.system.users

I am not sure why is it trying to find user - admin@bemeta, might be I am missing something over here, I do not have any such user but I do have an admin user which is used to connect to mongodb.

Here is the snapshot of my db (as shown in RockMongo):

enter image description here

Any ideas?

Devarshi
  • 16,440
  • 13
  • 72
  • 125
  • 2
    Did you try to authenticate the admin user for your `bemeta` db? You can do that in RockMongo: slect the db -> More -> Authentication -> Add user – Jiri Fiala Feb 24 '16 at 10:07
  • I have authenticated the admin user for db `bemta` but now on firing the get request I am getting this error: '502 Proxy Error The proxy server received an invalid response from an upstream server. The proxy server could not handle the request. GET /be_records Reason: Error reading from remote server – Devarshi Feb 24 '16 at 11:13
  • The `502 Proxy Error` error got resolved after I restarted the app on openshift :) Hey @JiriFiala why don't you add your comment as answer, I will mark it as accepted answer :) – Devarshi Feb 24 '16 at 11:21

1 Answers1

1

The auth error was caused by not having the admin user authenticated for accessing the bemeta database (see question comments above for details).

Users can be authenticated using the RockMongo interface as follows:
Select a DB -> More -> Authentication -> Add user.

Jiri Fiala
  • 1,400
  • 7
  • 10