AngularJS $http and REST servers : 'mongodb-rest',

238 views
Skip to first unread message

Yvon Thoraval

unread,
Jan 7, 2014, 4:30:18 AM1/7/14
to ang...@googlegroups.com
I've tried three kind of REST servers with bAngularJS $http :
 - NodeJS / mongodb-rest http://localhost:3000/test/users :
      http://localhost:3000/test/users
      [
        {
          "name": "Yvonec",
          "_id": "52c7d8e20dd0bc6d1c000001"
        },
        {
          "name": "Lilwenn",
          "_id": "52c7d8e20dd0bc6d1c000002"
        },
        {
          "_id": "52cb22116d3c20e905f23239",
          "name": "Toto"
        }
      ]


- Ruby   / DrowsyDromedary  http://localhost:9292/test/users :
[{"name":"Yvonec","_id":{"$oid": "52c7d8e20dd0bc6d1c000001"}},{"name":"Lilwenn","_id":{"$oid": "52c7d8e20dd0bc6d1c000002"}},{"_id":{"$oid": "52cb22116d3c20e905f23239"},"name":"Toto"}]

- Python / sleepy.mongoose http://localhost:27080/test/users/_find
{"ok": 1, "results": [{"_id": {"$oid": "52c7d8e20dd0bc6d1c000001"}, "name": "Yvonec"}, {"_id": {"$oid": "52c7d8e20dd0bc6d1c000002"}, "name": "Lilwenn"}, {"_id": {"$oid": "52cb22116d3c20e905f23239"}, "name": "Toto"}], "id": 0}

Despite different formats, the results, in a browser, are correct.

However, using AngularJS (I'm a newbie here) i get success only with Ruby DrowsyDromedary server.

here is my js code :

    var URL='http://localhost:3000/test/users';        // NodeJS / mongodb-rest
    var URL='http://localhost:9292/test/users';        // Ruby   / DrowsyDromedary
    //var URL='http://localhost:27080/test/users/_find'; // Python / sleepy.mongoose
    function RESTClient($scope, $http) {
      $scope.users=[];
      $http.get(URL).
        success(function(data, status) {
          console.log("success => typeof  data = "+typeof data);
          console.log("success =>   data = "+data);
          console.log("success => status = "+status);
          $scope.users = data; // get here with Ruby   / DrowsyDromedary server ONLY...
         })
        .error(function(data, status){
          console.log("error => typeof  data = "+typeof data);
          console.log("error =>   data = "+data);
          console.log("error => status = "+status);
        });
    }

Sander Elias

unread,
Jan 7, 2014, 5:47:12 AM1/7/14
to ang...@googlegroups.com
Hi Yvon,

I hear good things about restangular.  Never used it, as I write my own backend's. 
If you want to stay within angular, have a look at the $resource which is a wrapper on $http that handles REST.

If you need more information don't hesitate to ask!

Regards
Sander Elias

Yvon Thoraval

unread,
Jan 7, 2014, 6:03:12 AM1/7/14
to ang...@googlegroups.com

Fine thanks, i didn't mention I've also given a try using $resource and restangular, for the time being this is worst..

     angular.module('RESTTestApp', ['ngResource']);
    var URL='http://localhost:3000/test/users/52c7d8e20dd0bc6d1c000002';        // NodeJS / mongodb-rest

    var URL='http://localhost:9292/test/users';        // Ruby   / DrowsyDromedary
    //var URL='http://localhost:27080/test/users/_find'; // Python / sleepy.mongoose
    function RESTClient($scope, $resource) {
      $scope.URL=URL;
      $scope.data = $resource(URL,
        { callback:'JSON_CALLBACK'},
        {get:{method:'JSONP'}});
    
        $scope.users = $scope.data.get();
    }

for restangular, I'm not even ok reading the doc ))

    var URL='http://localhost:3000/test/users';        // NodeJS / mongodb-rest
    //var URL='http://localhost:9292/test/users';        // Ruby   / DrowsyDromedary

    //var URL='http://localhost:27080/test/users/_find'; // Python / sleepy.mongoose
    // Add Restangular as a dependency to your app
    angular.module('your-app', ['restangular']);

    // Inject Restangular into your controller
    angular.module('your-app').controller('MainCtrl', function($scope, Restangular) {
      $scope.URL=URL;
      var users = Restangular.all(URL);
      users.getList().then(function(users) {
        console.log("users = "+users);
        $scope.allUsers =users;
        $scope.user= $scope.allUsers[0];
      });
      // ...
    });


here i don't even get into ".then(function(users)"...

In fact all of those is to replace, eventually, my own Secure Web Socket server (written in js / NodeJS) working with mongodb (native).

Yvon Thoraval

unread,
Jan 7, 2014, 6:05:17 AM1/7/14
to ang...@googlegroups.com


Le mardi 7 janvier 2014 11:47:12 UTC+1, Sander Elias a écrit :

I should mention that some of the REST servers are more or less outadted, for example mongodb-rest is stucked since 2/3 years...

 

Sander Elias

unread,
Jan 7, 2014, 6:52:59 AM1/7/14
to ang...@googlegroups.com

Ah!

ok, can you try the following:

function RESTClient($scope, $resource) {
      $scope.URL=URL;
      $scope.data = $resource(URL,
        { callback:'JSON_CALLBACK'},
        {get:{method:'JSONP'}});

        $scope.data.get(function (x) {
           $scope.users = x;
           console.log(x)
        });
    }

Regards
Sander

Yvon Thoraval

unread,
Jan 7, 2014, 7:27:29 AM1/7/14
to ang...@googlegroups.com

2014/1/7 Sander Elias <sande...@gmail.com>

function RESTClient($scope, $resource) { $scope.URL=URL; $scope.data = $resource(URL, { callback:'JSON_CALLBACK'}, {get:{method:'JSONP'}}); $scope.data.get(function (x) { $scope.users = x; console.log(x) }); }

=============================================================================
Fine, it works whith Python / Sleepy.Mongoose.
Because http://localhost:27080/test/users/_find gives :
{"ok": 1, "results": [{"_id": {"$oid": "52c7d8e20dd0bc6d1c000001"}, "name": "Yvonec"}, {"_id": {"$oid": "52c7d8e20dd0bc6d1c000002"}, "name": "Lilwenn"}, {"_id": {"$oid": "52cb22116d3c20e905f23239"}, "name": "Toto"}], "id": 2}
I've changed a bit the js :
...
               $scope.users = x.results;
               console.log("x = "+x)
               console.log("x.results = "+x.results)
...
log at server side :
$ rackup
[2014-01-07 12:55:47] INFO  WEBrick 1.3.1
[2014-01-07 12:55:47] INFO  ruby 2.0.0 (2013-02-08) [x86_64-darwin12.0.0]
[2014-01-07 12:55:47] INFO  WEBrick::HTTPServer#start: pid=4259 port=9292
127.0.0.1 - - [07/Jan/2014 12:56:08] "GET /test/users?callback=angular.callbacks._0 HTTP/1.1" 200 183 0.0781
127.0.0.1 - - [07/Jan/2014 12:57:00] "GET /test/users?callback=angular.callbacks._0 HTTP/1.1" 200 183 0.0458
127.0.0.1 - - [07/Jan/2014 12:57:09] "GET /test/users?callback=angular.callbacks._0 HTTP/1.1" 200 183 0.0140
127.0.0.1 - - [07/Jan/2014 12:57:44] "GET /test/users?callback=angular.callbacks._0 HTTP/1.1" 200 183 0.0268
127.0.0.1 - - [07/Jan/2014 13:07:08] "GET /test/users HTTP/1.1" 200 183 0.0188
127.0.0.1 - - [07/Jan/2014 13:09:08] "GET /test/users?callback=angular.callbacks._0 HTTP/1.1" 200 183 0.0584

=============================================================================with Ruby   / DrowsyDromedary :

http://localhost:9292/test/users
[{"name":"Yvonec","_id":{"$oid": "52c7d8e20dd0bc6d1c000001"}},{"name":"Lilwenn","_id":{"$oid": "52c7d8e20dd0bc6d1c000002"}},{"_id":{"$oid": "52cb22116d3c20e905f23239"},"name":"Toto"}]

Resulting in NOTHING at console.log...

log at the server side :
$ python httpd.py

=================================
| MongoDB REST Server |
=================================

listening for connections on http://localhost:27080

1.0.0.127.in-addr.arpa - - [07/Jan/2014 12:59:45] "GET /test/users/_find?callback=angular.callbacks._0 HTTP/1.1" 200 -
1.0.0.127.in-addr.arpa - - [07/Jan/2014 13:00:23] "GET /test/users/_find HTTP/1.1" 200 -
1.0.0.127.in-addr.arpa - - [07/Jan/2014 13:01:38] "GET /test/users/_find HTTP/1.1" 200 -
1.0.0.127.in-addr.arpa - - [07/Jan/2014 13:01:51] "GET /test/users/_find?callback=angular.callbacks._0 HTTP/1.1" 200 -
1.0.0.127.in-addr.arpa - - [07/Jan/2014 13:02:33] "GET /test/users/_find?callback=angular.callbacks._0 HTTP/1.1" 200 -


=============================================================================
and finally with NodeJS / mongodb-rest :
http://localhost:3000/test/users

[ { "name": "Yvonec", "_id": "52c7d8e20dd0bc6d1c000001" }, { "name": "Lilwenn", "_id": "52c7d8e20dd0bc6d1c000002" }, { "_id": "52cb22116d3c20e905f23239", "name": "Toto" } ]

Resulting in NOTHING at console.log...

server log being :
$ mongodb-rest
Warning: express.createServer() is deprecated, express
applications no longer inherit from http.Server,
please use:

var express = require("express");
var app = express();

connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
========================================================================================
= Please ensure that you set the default write concern for the database by setting =
= one of the options =
= =
= w: (value of > -1 or the string 'majority'), where < 1 means =
= no write acknowledgement =
= journal: true/false, wait for flush to journal before acknowledgement =
= fsync: true/false, wait for flush to file system before acknowledgement =
= =
= For backward compatibility safe is still supported and =
= allows values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}] =
= the default value is false which means the driver receives does not =
= return the information of the success/error of the insert/update/remove =
= =
= ex: new Db(new Server('localhost', 27017), {safe:false}) =
= =
= http://www.mongodb.org/display/DOCS/getLastError+Command =
= =
= The default of no acknowledgement will change in the very near future =
= =
= This message will disappear when the default safe is set on the driver Db =
========================================================================================
127.0.0.1 - - [Tue, 07 Jan 2014 12:13:14 GMT] "GET /test/users HTTP/1.1" 304 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:29.0) Gecko/20100101 Firefox/29.0"

However looking the repos I've found :

Ruby   / DrowsyDromedary
latest change drowsy_dromedary.rb 5 months ago


Python / Sleepy.Mongoose
latest change is a year ago about Readme.md and 2 years ago for sleepymongoose.

NodeJS / mongodb-rest
still states :
MongoDB Rest is an alpha REST interface to MongoDB that uses the MongoDB Node Native driver.







may be the REST specification itself isn't the same...

ANyway, thanks a lot for your help !

--
Yvon@48.871651804,2.384858688

Sander Elias

unread,
Jan 7, 2014, 10:17:21 AM1/7/14
to ang...@googlegroups.com
Yvon,

You are welcome. and indeed the REST specification leaves quite a lot up to the implementors. early adaptations vary even more than moderner ones. As far as I know it isn't even a formal standard. 
May I ask why you are testing on those different platforms? Are you looking for an suitable backend? do you need a specific database?

Regards
Sander

Yvon Thoraval

unread,
Jan 7, 2014, 10:57:50 AM1/7/14
to ang...@googlegroups.com
I'm using MongoDB and i thought REST was standardized enough, REST looking as a better choice in place of my own websocket where all the requests are of JSON.

I was testing different platforms (I'm coming from ruby side) because i didn't know clearly if those issues were coming me and/or the mismatch between REST server and client.

clearly now, it was both.

Obviously i'd prefer everything being in JS...

And the result, at this time is Ruby on the server side, js the client one.

I do have another test to work with: change setting of MongoDB to be RESTfull and try again.
This would implied authentification rewriting.




2014/1/7 Sander Elias <sande...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/uf_hoX9W8P8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.



--
Yvon@48.871651804,2.384858688

Sander Elias

unread,
Jan 7, 2014, 1:36:45 PM1/7/14
to ang...@googlegroups.com
Yvon,

Ah, I see. a little while ago I wrote a super simple rest interface in node for an demo. It is utilizing an json storage. There is not that much too it.  If you are interested I can see if I can still find that code. It is demo code, so not battle tested though!
Beside that, there is a world of things in between sending off everything via websockets and a full rest interface. I don't know how the mongoDB <-> nodejs interface is, but if you want full stack JS, it is certainly possible.

Regards
Sander

Yvon Thoraval

unread,
Jan 7, 2014, 2:10:20 PM1/7/14
to ang...@googlegroups.com
MongoDB is written in js then the interface is pretty straightforward.
remember with MongoDB even with php, u can make use of `this`... and also jj regex.

I gave a try with MongoDB REST Console (http native)
it works well from a browser

but i was unable to connect using angular $http nor $resourse, the url must look like that :
http://server:port/database/collection/
with trailing a "/"

at least, i've seen from browser the trailing "/" is eated by angular...
and also i got back 0 kb...

i'll give another tomorrow morning it's 8H eve here.



2014/1/7 Sander Elias <sande...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/uf_hoX9W8P8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.



--
Yvon@48.871651804,2.384858688

Reply all
Reply to author
Forward
0 new messages