Cannot POST /api/users using loopback-connector-rest

1,161 views
Skip to first unread message

Julian Mayorga

unread,
Jul 13, 2014, 11:05:47 PM7/13/14
to loopb...@googlegroups.com
Hello kind folks from the lovely loopback community,

Here's my situation, I setup a small repository to test connection between two loopback apps using loopback-connector-rest. I created both apps with slc lb project, so I am sure you are familiar with their structure.

I have two apps, a master app with a User model using a memory datasource, and a slave app with a User model with a loopback-connector-rest datasource and a baseUrl that points towards master app's url. You can check out the repository on github: https://github.com/JulianMayorga/rest-connector-example

The problem is that with this setup I cannot do a POST to [slave_url]/api/users, even though I can do that with the master's url. I defined a test that proves I cannot do this:

'use strict';

var request = require('supertest');
var should = require('should');

var master = require('../master/app.js');
var slave = require('../slave/app.js');

describe('Master server', function () {
  var email = 'em...@example.com';
  var password = '123456';

  it('should return new user email and id', function (done) {
    request(master)
    .post('/api/users')
    .send({
      email: email,
      password: password
    })
    .expect(200)
    .end(function (err, res) {
      res.body.email.should.equal(email);
      res.body.id.should.be.an.instanceOf(Number);
      done();
    });
  });
});

describe('Slave server', function () {
  var email = 'anothe...@example.com';
  var password = '123456';

  it('should return new user email and id', function (done) {
    request(slave)
    .post('/api/users')
    .send({
      email: email,
      password: password
    })
    .expect(200)
    .end(function (err, res) {
      res.body.email.should.equal(email);
      res.body.id.should.be.an.instanceOf(Number);
      done();
    });
  });
}); 

And this is the output I get when running mocha:

POST /api/users 200 703ms - 45b
․․

  1 passing (3s)
  1 failing

  1) Slave server should return new user email and id:
     Error: timeout of 2000ms exceeded

In order to figure out what's happening here I tried to debug the app by running DEBUG=loopback:connector:rest node app.js in slave app, which outputs the following debug message:

POST /api/users 200 2015ms
  loopback:connector:rest Request: {"method":"GET","uri":"http://localhost:3000/api/users","json":true,"qs":{"where":{"email":"anothe...@example.com"}}} +25s

So then I checked out the logs in master server, and it is effectively receiving a GET request:

GET /api/users?where[email]=anotheremail%40example.com 401 6ms - 1.06kb

I don't know why, but rest connector fires a GET request instead of a POST when I try to create a user. Does anyone know why this might happen?

I just want to have a central app that stores users and accessTokens, and secondary apps that have their own models but use the central app for users and tokens.

Thanks so much in advance,
Julián.

Raymond Feng

unread,
Jul 14, 2014, 11:20:53 AM7/14/14
to Julian Mayorga, loopb...@googlegroups.com
For REST connector, you will have to configure operations with HTTP methods. See http://docs.strongloop.com/display/LB/REST+connector.

In your case, if you just want to proxy LoopBack models running on a different server, ‘remote’ connector is a better choice.


It allows you to transparently interact with a remote LoopBack model.
 
Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julian Mayorga

unread,
Jul 14, 2014, 5:03:18 PM7/14/14
to loopb...@googlegroups.com, mayorga...@gmail.com
Thanks Raymond! I got it working with remote connector.

I have a doubt related to remote connector. I had to set it restApiRoot in my app to '/' instead of '/api' so that it works.

{
  "port": 3000,
  "host": "0.0.0.0",
  "cookieSecret": "710dacf8-b1af-4baf-91e8-dc7bbcd0c956",
  "restApiRoot": "/"
}

How do you configure the remote connector so that it recognizes loopback's default restApiRoute? 

  "remote": {
    "defaultForType": "remote",
    "connector": "remote",
    "host": "localhost",
    "port": 3000
  }

Raymond Feng

unread,
Jul 14, 2014, 5:10:46 PM7/14/14
to Julian Mayorga, loopb...@googlegroups.com
You can configure the remote connector as follows:

{
"remote”: { 
  "defaultForType": “remote”,
  "connector": "remote”,
  "host": "localhost”,
  "port": 3000,
  “root”: “/api"
  }
}

or 

{
"remote”: { 
  "defaultForType": “remote”,
  "connector": "remote”,
  }
}

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

Julian Mayorga

unread,
Jul 14, 2014, 5:13:34 PM7/14/14
to loopb...@googlegroups.com, mayorga...@gmail.com
Cool, that'll do it! I'm sorry for troubling you, I guess I have to level up my code-reading skills.

Thanks,
Reply all
Reply to author
Forward
0 new messages