Search code examples
javascriptnode.jsloopbackjsstrongloop

Remove routes from loopback


I'm starting to use loopback to create my API and i'm facing a question: is there a way to remove routes from loopback ?

Let say I have an Admin model and i want to add a custom route (like admin/login) on which i send my username and password and it returns me "ok" if that's good. But i don't want to have all of theses routes like count, change-stream, etc...

How can I remove them ? I've been searching on Google but there's no answers that corresponds to my issue..

Thanks in advance for your response !


Solution

  • An old issue has been opened here on github: https://github.com/strongloop/loopback/issues/651

    Here's the long answer from @dancingshell:

    use strict';
    const
      relationMethodPrefixes = [
        'prototype.__findById__',
        'prototype.__destroyById__',
        'prototype.__updateById__',
        'prototype.__exists__',
        'prototype.__link__',
        'prototype.__get__',
        'prototype.__create__',
        'prototype.__update__',
        'prototype.__destroy__',
        'prototype.__unlink__',
        'prototype.__count__',
        'prototype.__delete__'
      ];
    
    function reportDisabledMethod( model, methods ) {
      const joinedMethods = methods.join( ', ' );
    
      if ( methods.length ) {
        console.log( '\nRemote methods hidden for', model.sharedClass.name, ':', joinedMethods, '\n' );
      }
    }
    
    module.exports = {
      disableAllExcept( model, methodsToExpose ) {
        const
          excludedMethods = methodsToExpose || [];
        var hiddenMethods = [];
    
        if ( model && model.sharedClass ) {
          model.sharedClass.methods().forEach( disableMethod );
          Object.keys( model.definition.settings.relations ).forEach( disableRelatedMethods );
          reportDisabledMethod( model, hiddenMethods );
        }
        function disableRelatedMethods( relation ) {
          relationMethodPrefixes.forEach( function( prefix ) {
            var methodName = prefix + relation;
    
            disableMethod({ name: methodName });
          });
        }
        function disableMethod( method ) {
          var methodName = method.name;
    
          if ( excludedMethods.indexOf( methodName ) < 0 ) {
            model.disableRemoteMethodByName( methodName );
            hiddenMethods.push( methodName );
          }
        }
      },
      /**
       * Options for methodsToDisable:
       * create, upsert, replaceOrCreate, upsertWithWhere, exists, findById, replaceById,
       * find, findOne, updateAll, deleteById, count, updateAttributes, createChangeStream
       * -- can also specify related method using prefixes listed above
       * and the related model name ex for Account: (prototype.__updateById__followers, prototype.__create__tags)
       * @param model
       * @param methodsToDisable array
       */
      disableOnlyTheseMethods( model, methodsToDisable ) {
        methodsToDisable.forEach( function( method ) {
          model.disableRemoteMethodByName( method );
        });
        reportDisabledMethod( model, methodsToDisable );
      }
    };
    

    But I recommend to use the custom mixin's plugin made by @c3s4r:

    https://www.npmjs.com/package/loopback-setup-remote-methods-mixin