【问题标题】:Remove routes from loopback从环回中删除路由
【发布时间】:2017-04-19 18:24:45
【问题描述】:

我开始使用环回来创建我的 API,我面临一个问题:有没有办法从环回中删除路由?

假设我有一个 Admin 模型,我想添加一个自定义路由(如 admin/login),我在该路由上发送我的用户名和密码,如果这很好,它会返回“ok”。但我不想拥有所有这些路线,如计数、更改流等......

我怎样才能删除它们?我一直在 Google 上搜索,但没有与我的问题相对应的答案..

提前感谢您的回复!

【问题讨论】:

    标签: javascript node.js loopbackjs strongloop


    【解决方案1】:

    github上已经打开了一个老问题:https://github.com/strongloop/loopback/issues/651

    这是来自@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 );
      }
    };
    

    但我推荐使用@c3s4r 制作的自定义mixin插件:

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

    【讨论】:

      猜你喜欢
      • 2016-03-25
      • 2023-03-29
      • 2018-04-27
      • 2017-05-12
      • 2015-11-17
      • 1970-01-01
      • 2023-01-20
      • 2020-01-16
      • 2021-12-27
      相关资源
      最近更新 更多