0
votes

For instance: In order to create a new user the standard path is POST /api/users/.

this invokes the create remote hook, etc.

How is it possible to change the standard path to something like POST /api/users/new/ and still retain current (and correct) functionality? Thanks.

Alternatively is it possible to replicate this functionality with a new remote method user.new()? How would that look?

1

1 Answers

1
votes

Add a remote method user.new and call User.create() within it yourself.

I'm assuming that you're starting with the (lowercase) user.js and user.json model that is an extension of the built-in (uppercase) User model that ships with loopback.

In user.js, something like this:

module.exports = function(user) {

  user.remoteMethod('new',
    {
      accepts: [
        {arg: 'userInfo', type: 'object'}
      ],
      returns: {
        arg: 'success',
        type: 'boolean'
      }
    }
  );

  user.new = function(userInfo, cb) {

    user.create(userInfo, function(err, newUser) {
      if(err) return cb(err, null);

      return cb(null, true);

    });

  };

};

You can also modify the REST API http url path structure with an additional http property on the remote method specification. This is not strictly necessary since the method will by default take the name of the method. Would be cool if you could just modify this to override the behavior of the built-ins, but I haven't tested if that's possible. You can also enforce POSTing:

user.remoteMethod('new',
  {
    http: {path: '/new', verb: 'post'}, // <--
    accepts: [
      {arg: 'userInfo', type: 'object'}
    ],
    returns: {
      arg: 'success',
      type: 'boolean'
    }
  }
);