项目环回中的自定义连接器

时间:2019-07-23 09:36:29

标签: node.js rabbitmq loopbackjs

我正在尝试创建自定义环回(3.25.1)连接器

我不需要使用外部模块创建连接器,这是一个非常简单的连接Rabbitmq的连接器。

我在项目的根目录connectors/rabbitmq.js中有一个文件

rabbitmq.js的内容是

const Connector = require('loopback-connector').Connector;

/**
 * Initialize the  connector against the given data source
 *
 * @param {DataSource} dataSource The loopback-datasource-juggler dataSource
 * @param {Function} [callback] The callback function
 */
exports.initialize = function initializeDataSource(dataSource, callback) {
  dataSource.connector = new RabbitMQ(dataSource.settings);
};

function RabbitMQ(settings) {
  // Call the super constructor with name and settings
  Connector.call(this, 'rabbitmq', settings);
  //...
}

RabbitMQ.prototype.connect = function (cb) {
  // ...
};

RabbitMQ.prototype.disconnect = function (cb) {
  // ...
};

开始我的项目时,出现以下错误消息

 WARNING: LoopBack connector "rabbitmq" is not installed as any of the following modules:

  ./connectors/rabbitmq
 loopback-connector-rabbitmq
 rabbitmq

 To fix, run:

     npm install rabbitmq --save

 /usr/src/app/node_modules/loopback/lib/application.js:280
     throw err;
     ^

 Error: Cannot create data source "rabbitmq":
 WARNING: LoopBack connector "rabbitmq" is not installed as any of the following modules:

  ./connectors/rabbitmq
 loopback-connector-rabbitmq
 rabbitmq

 To fix, run:

     npm install rabbitmq --save

     at DataSource.setup (/usr/src/app/node_modules/loopback-datasource-juggler/lib/datasource.js:448:26)
     at new DataSource (/usr/src/app/node_modules/loopback-datasource-juggler/lib/datasource.js:138:8)
     at Registry.createDataSource (/usr/src/app/node_modules/loopback/lib/registry.js:364:12)
     at dataSourcesFromConfig (/usr/src/app/node_modules/loopback/lib/application.js:570:19)
     at Function.app.dataSource (/usr/src/app/node_modules/loopback/lib/application.js:269:14)
     at /usr/src/app/node_modules/loopback-boot/lib/executor.js:191:9
     at /usr/src/app/node_modules/loopback-boot/lib/executor.js:282:5
     at Array.forEach (<anonymous>)
     at forEachKeyedObject (/usr/src/app/node_modules/loopback-boot/lib/executor.js:281:20)
     at setupDataSources (/usr/src/app/node_modules/loopback-boot/lib/executor.js:181:3)
     at execute (/usr/src/app/node_modules/loopback-boot/lib/executor.js:39:3)
     at bootLoopBackApp (/usr/src/app/node_modules/loopback-boot/index.js:154:3)
     at Object.<anonymous> (/usr/src/app/server/server.js:63:1)
     at Module._compile (internal/modules/cjs/loader.js:776:30)
     at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
     at Module.load (internal/modules/cjs/loader.js:653:32)
 Emitted 'error' event at:
     at DataSource.setup (/usr/src/app/node_modules/loopback-datasource-juggler/lib/datasource.js:448:12)
     at new DataSource (/usr/src/app/node_modules/loopback-datasource-juggler/lib/datasource.js:138:8)
     [... lines matching original stack trace ...]
     at Module.load (internal/modules/cjs/loader.js:653:32)
 npm ERR! code ELIFECYCLE
 npm ERR! errno 1
 npm ERR! service-rest@1.0.0 fixtures: `node bin/fixtures/index.js`
 npm ERR! Exit status 1
 npm ERR!
 npm ERR! Failed at the service-rest@1.0.0 fixtures script.
 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

 npm ERR! A complete log of this run can be found in:
 npm ERR!     /root/.npm/_logs/2019-07-23T09_31_48_754Z-debug.log
 npm ERR! code ELIFECYCLE
 npm ERR! errno 1
 npm ERR! service-rest@1.0.0 launch: `npm run fixtures && npm start`
 npm ERR! Exit status 1
 npm ERR!
 npm ERR! Failed at the service-rest@1.0.0 launch script.
 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

 npm ERR! A complete log of this run can be found in:
 npm ERR!     /root/.npm/_logs/2019-07-23T09_31_48_808Z-debug.log

我的文件位于./connectors/rabbitmq中,我尝试了以下路径,但始终遇到相同的问题

connectors/rabbitmq.js connectors/rabbitmq/index.js server/connectors/rabbitmq.js server/connectors/rabbitmq/index.js

我也尝试添加

app.connector('rabbitmq', require('../connectors/rabbitmq'))var app = module.exports = loopback();server/server.js之后,但我遇到相同的问题

2 个答案:

答案 0 :(得分:0)

创建一个文件夹./connectors/rabbitmq并在其中进行npm init。然后,在package.json中确保您拥有"main": "index.js",。这样就可以了。

请参阅模块示例。 https://github.com/edvisor-io/loopback-connector-virtual

答案 1 :(得分:0)

我找到了解决方法:

需要添加

app.connector('rabbitmq', require('../connectors/rabbitmq'))

不要忘记name中的connectordatasources.json