我有下面的js类 KafkaServices
. 在 server.js
我想从中调用一个方法 KafkaServices
创建Kafka连接。在 server.js
我现在就是这样做到的:
/**
* Event listener for HTTP server "listening" event.
*/
var kafkaServices = require('./services/kafka-services');
const kafka = require('kafka-node');
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
logInfo({message: `Server is Listening on http://localhost:${addr.port}.`, scope: 'Server'});
kafkaTool = new kafkaServices.KafkaServices();
// Connect to kafka
kafkaTool.connect();
kafkaTool.error();
}
我的 KafkaServices
也在下面。我在用 module.exports = KafkaServices;
导出类并能够在其他地方使用它。
var express = require('express');
var kafka = require('kafka-node');
class KafkaServices{
constructor(){
this.Producer = kafka.Producer,
this.client = new kafka.Client();
this.producer = new Producer(client);
}
connect(){
// Create a connection
this.producer.on('ready', function() {
console.log('Producer is ready');
})
}
error(){
this.producer.on('error', function(err) {
console.log('Producer has the following error');
console.log(err);
})
}
publish(){
//Publish
this.producer.send(payload, function (err, data){})
}
}
module.exports = KafkaServices;
当我尝试运行此程序时,会收到以下错误消息:
PS C:\Users\ENV\Projects\tool> npm run start
> settings-tool@1.0.0 start C:\Users\ENV\Projects\tool
> node ./src/server.js
Listening on port 3999
{"timestamp":"2020-07-02T18:22:27.909Z","level":"info","app_name":"tool","message":"Server is Listening on http://localhost:3999.","scope":"Server","tag":"tool"}
{"error":{},"level":"error","message":"uncaughtException: kafkaServices.KafkaServices is not a constructor\nTypeError: kafkaServices.KafkaServices is not a constructor\n at Server.onListening (C:\\Users\\ENV\\Projects\\tool\\src\\server.js:149:17)\n at Server.emit (events.js:198:13)\n at emitListeningNT (net.js:1313:10)\n at process._tickCallback (internal/process/next_tick.js:63:19)\n at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)\n at startup (internal/bootstrap/node.js:283:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","stack":"TypeError: kafkaServices.KafkaServices is not a constructor\n at Server.onListening (C:\\Users\\ENV\\Projects\\tool\\src\\server.js:149:17)\n at Server.emit (events.js:198:13)\n at emitListeningNT (net.js:1313:10)\n at process._tickCallback (internal/process/next_tick.js:63:19)\n at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)\n at startup (internal/bootstrap/node.js:283:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","exception":true,"date":"Thu Jul 02 2020 14:22:27 GMT-0400 (Eastern Daylight Time)","process":{"pid":22460,"uid":null,"gid":null,"cwd":"C:\\Users\\ENV\\Projects\\tool","execPath":"C:\\Program Files\\nodejs\\node.exe","version":"v10.16.3","argv":["C:\\Program Files\\nodejs\\node.exe","C:\\Users\\ENV\\Projects\\tool\\src\\server.js"],"memoryUsage":{"rss":40157184,"heapTotal":25980928,"heapUsed":16159328,"external":134547}},"os":{"loadavg":[0,0,0],"uptime":106081},"trace":[{"column":17,"file":"C:\\Users\\ENV\\Projects\\tool\\src\\server.js","function":"Server.onListening","line":149,"method":"onListening","native":false},{"column":13,"file":"events.js","function":"Server.emit","line":198,"method":"emit","native":false},{"column":10,"file":"net.js","function":"emitListeningNT","line":1313,"method":null,"native":false},{"column":19,"file":"internal/process/next_tick.js","function":"process._tickCallback","line":63,"method":"_tickCallback","native":false},{"column":11,"file":"internal/modules/cjs/loader.js","function":"Module.runMain","line":834,"method":"runMain","native":false},{"column":19,"file":"internal/bootstrap/node.js","function":"startup","line":283,"method":null,"native":false},{"column":3,"file":"internal/bootstrap/node.js","function":"bootstrapNodeJSCore","line":622,"method":null,"native":false}],"tag":"tool"}
{"error":{},"level":"error","message":"uncaughtException: kafkaServices.KafkaServices is not a constructor\nTypeError: kafkaServices.KafkaServices is not a constructor\n at Server.onListening (C:\\Users\\ENV\\Projects\\tool\\src\\server.js:149:17)\n at Server.emit (events.js:198:13)\n at emitListeningNT (net.js:1313:10)\n at process._tickCallback (internal/process/next_tick.js:63:19)\n at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)\n at startup (internal/bootstrap/node.js:283:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","stack":"TypeError: kafkaServices.KafkaServices is not a constructor\n at Server.onListening (C:\\Users\\ENV\\Projects\\tool\\src\\server.js:149:17)\n at Server.emit (events.js:198:13)\n at emitListeningNT (net.js:1313:10)\n at process._tickCallback (internal/process/next_tick.js:63:19)\n at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)\n at startup (internal/bootstrap/node.js:283:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","exception":true,"date":"Thu Jul 02 2020 14:22:27 GMT-0400 (Eastern Daylight Time)","process":{"pid":22460,"uid":null,"gid":null,"cwd":"C:\\Users\\ENV\\Projects\\tool","execPath":"C:\\Program Files\\nodejs\\node.exe","version":"v10.16.3","argv":["C:\\Program Files\\nodejs\\node.exe","C:\\Users\\ENV\\Projects\\tool\\src\\server.js"],"memoryUsage":{"rss":41377792,"heapTotal":25980928,"heapUsed":16255176,"external":134547}},"os":{"loadavg":[0,0,0],"uptime":106081},"trace":[{"column":17,"file":"C:\\Users\\ENV\\Projects\\tool\\src\\server.js","function":"Server.onListening","line":149,"method":"onListening","native":false},{"column":13,"file":"events.js","function":"Server.emit","line":198,"method":"emit","native":false},{"column":10,"file":"net.js","function":"emitListeningNT","line":1313,"method":null,"native":false},{"column":19,"file":"internal/process/next_tick.js","function":"process._tickCallback","line":63,"method":"_tickCallback","native":false},{"column":11,"file":"internal/modules/cjs/loader.js","function":"Module.runMain","line":834,"method":"runMain","native":false},{"column":19,"file":"internal/bootstrap/node.js","function":"startup","line":283,"method":null,"native":false},{"column":3,"file":"internal/bootstrap/node.js","function":"bootstrapNodeJSCore","line":622,"method":null,"native":false}],"tag":"tool"}
{"error":{},"level":"error","message":"uncaughtException: kafkaServices.KafkaServices is not a constructor\nTypeError: kafkaServices.KafkaServices is not a constructor\n at Server.onListening (C:\\Users\\ENV\\Projects\\tool\\src\\server.js:149:17)\n at Server.emit (events.js:198:13)\n at emitListeningNT (net.js:1313:10)\n at process._tickCallback (internal/process/next_tick.js:63:19)\n at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)\n at startup (internal/bootstrap/node.js:283:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","stack":"TypeError: kafkaServices.KafkaServices is not a constructor\n at Server.onListening (C:\\Users\\ENV\\Projects\\tool\\src\\server.js:149:17)\n at Server.emit (events.js:198:13)\n at emitListeningNT (net.js:1313:10)\n at process._tickCallback (internal/process/next_tick.js:63:19)\n at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)\n at startup (internal/bootstrap/node.js:283:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","exception":true,"date":"Thu Jul 02 2020 14:22:27 GMT-0400 (Eastern Daylight Time)","process":{"pid":22460,"uid":null,"gid":null,"cwd":"C:\\Users\\ENV\\Projects\\tool","execPath":"C:\\Program Files\\nodejs\\node.exe","version":"v10.16.3","argv":["C:\\Program Files\\nodejs\\node.exe","C:\\Users\\ENV\\Projects\\tool\\src\\server.js"],"memoryUsage":{"rss":41385984,"heapTotal":25980928,"heapUsed":16300016,"external":142739}},"os":{"loadavg":[0,0,0],"uptime":106081},"trace":[{"column":17,"file":"C:\\Users\\ENV\\Projects\\tool\\src\\server.js","function":"Server.onListening","line":149,"method":"onListening","native":false},{"column":13,"file":"events.js","function":"Server.emit","line":198,"method":"emit","native":false},{"column":10,"file":"net.js","function":"emitListeningNT","line":1313,"method":null,"native":false},{"column":19,"file":"internal/process/next_tick.js","function":"process._tickCallback","line":63,"method":"_tickCallback","native":false},{"column":11,"file":"internal/modules/cjs/loader.js","function":"Module.runMain","line":834,"method":"runMain","native":false},{"column":19,"file":"internal/bootstrap/node.js","function":"startup","line":283,"method":null,"native":false},{"column":3,"file":"internal/bootstrap/node.js","function":"bootstrapNodeJSCore","line":622,"method":null,"native":false}],"tag":"tool"}
{"error":{},"level":"error","message":"uncaughtException: kafkaServices.KafkaServices is not a constructor\nTypeError: kafkaServices.KafkaServices is not a constructor\n at Server.onListening (C:\\Users\\ENV\\Projects\\tool\\src\\server.js:149:17)\n at Server.emit (events.js:198:13)\n at emitListeningNT (net.js:1313:10)\n at process._tickCallback (internal/process/next_tick.js:63:19)\n at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)\n at startup (internal/bootstrap/node.js:283:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","stack":"TypeError: kafkaServices.KafkaServices is not a constructor\n at Server.onListening (C:\\Users\\ENV\\Projects\\tool\\src\\server.js:149:17)\n at Server.emit (events.js:198:13)\n at emitListeningNT (net.js:1313:10)\n at process._tickCallback (internal/process/next_tick.js:63:19)\n at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)\n at startup (internal/bootstrap/node.js:283:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","exception":true,"date":"Thu Jul 02 2020 14:22:27 GMT-0400 (Eastern Daylight Time)","process":{"pid":22460,"uid":null,"gid":null,"cwd":"C:\\Users\\ENV\\Projects\\tool","execPath":"C:\\Program Files\\nodejs\\node.exe","version":"v10.16.3","argv":["C:\\Program Files\\nodejs\\node.exe","C:\\Users\\ENV\\Projects\\tool\\src\\server.js"],"memoryUsage":{"rss":41385984,"heapTotal":25980928,"heapUsed":16340888,"external":142739}},"os":{"loadavg":[0,0,0],"uptime":106081},"trace":[{"column":17,"file":"C:\\Users\\ENV\\Projects\\tool\\src\\server.js","function":"Server.onListening","line":149,"method":"onListening","native":false},{"column":13,"file":"events.js","function":"Server.emit","line":198,"method":"emit","native":false},{"column":10,"file":"net.js","function":"emitListeningNT","line":1313,"method":null,"native":false},{"column":19,"file":"internal/process/next_tick.js","function":"process._tickCallback","line":63,"method":"_tickCallback","native":false},{"column":11,"file":"internal/modules/cjs/loader.js","function":"Module.runMain","line":834,"method":"runMain","native":false},{"column":19,"file":"internal/bootstrap/node.js","function":"startup","line":283,"method":null,"native":false},{"column":3,"file":"internal/bootstrap/node.js","function":"bootstrapNodeJSCore","line":622,"method":null,"native":false}],"tag":"tool"}
{"timestamp": "2020-07-02T18:22:27.959Z","message":"Uncaught Exception.","scope":"ServerError","error":"TypeError. kafkaServices.KafkaServices is not a constructor", "stack":"TypeError: kafkaServices.KafkaServices is not a constructor
at Server.onListening (C:\Users\ENV\Projects\tool\src\server.js:149:17)
at Server.emit (events.js:198:13)
at emitListeningNT (net.js:1313:10)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","origin":"undefined"}
{"timestamp":"2020-07-02T18:22:27.961Z","level":"error","app_name":"tool","message":"uncaughtException Error Message: kafkaServices.KafkaServices is not a constructor","label":{"label":"ServerError","scope":"ServerError"},"error_stack":"TypeError: kafkaServices.KafkaServices is not a constructor\n at Server.onListening (C:\\Users\\ENV\\Projects\\tool\\src\\server.js:149:17)\n at Server.emit (events.js:198:13)\n at emitListeningNT (net.js:1313:10)\n at process._tickCallback (internal/process/next_tick.js:63:19)\n at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)\n at startup (internal/bootstrap/node.js:283:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)","tag":"tool"}
{"timestamp":"2020-07-02T18:22:30.943Z","level":"info","app_name":"tool","message":"Process exit event with code 1.","scope":"Server","tag":"tool"}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! settings-tool@1.0.0 start: `node ./src/server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the settings-tool@1.0.0 start 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! C:\Users\ENV\AppData\Roaming\npm-cache\_logs\2020-07-02T18_22_30_969Z-debug.log
我不太清楚为什么我会犯这个错误,因为 KafkaServices
有一个构造函数。任何建议都将不胜感激!谢谢
1条答案
按热度按时间nqwrtyyt1#
kafkaservices构造函数应如下所示:
还有,改变
server.js
如下所示解决了此特定错误。