简体   繁体   English

从环回删除路由

[英]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 ? 我开始使用环回创建我的API,并且遇到一个问题:是否可以从环回中删除路由?

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. 假设我有一个Admin模型,我想添加一个自定义路由(例如admin / login),在该路由上发送我的用户名和密码,如果很好,它将返回“ ok”。 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.. 我一直在Google上搜索,但没有与我的问题相对应的答案。

Thanks in advance for your response ! 预先感谢您的回复!

An old issue has been opened here on github: https://github.com/strongloop/loopback/issues/651 在github上已打开一个旧问题: https : //github.com/strongloop/loopback/issues/651

Here's the long answer from @dancingshell: 这是@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: 但是我建议使用@ c3s4r制作的自定义mixin插件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM