gpt4 book ai didi

node.js - 项目环回内的自定义连接器

转载 作者:搜寻专家 更新时间:2023-10-31 23:25:58 24 4
gpt4 key购买 nike

我正在尝试创建自定义环回 (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 中,我尝试了以下路径,但我总是遇到同样的问题

连接器/rabbitmq.js连接器/rabbitmq/index.js服务器/连接器/rabbitmq.js服务器/连接器/rabbitmq/index.js

我也试过添加

app.connector('rabbitmq', require('../connectors/rabbitmq')) 就在 var app = module.exports = loopback(); 之后在 server/server.js 但我有同样的问题

最佳答案

创建文件夹 ./connectors/rabbitmq 并在其中执行 npm init。然后在 package.json 中确保您有 "main": "index.js",。这应该工作。

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

关于node.js - 项目环回内的自定义连接器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57161112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com