NodeJS 包名意外的标识符

cld4siwp  于 2023-05-17  发布在  Node.js
关注(0)|答案(1)|浏览(185)

我找不到与我相同或相近的问题。我在做一个RFID gRPC服务。我的包名是rfidgrpc,它在项目中是唯一的。但我仍然得到意外的标识符错误。顺便说一句,我是新的与这个gRPC的事情。以下是我的proto:

syntax = "proto3";

package rfidgrpc;

service RFIDService {
  rpc ReadRFID (RFIDRequest) returns (RFIDResponse) {}
}

message RFIDRequest {}

message RFIDResponse {
  string tag = 1;
}

下面是我的server.js:

const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const SerialPort = require('serialport').SerialPort;
const {ReadlineParser} = require('@serialport/parser-readline');
var PROTO_PATH = `${__dirname}/rfid.proto`;
const RFIDService = require('./RFIDService');

// Load the protobuf file
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });

// Load the gRPC package
//const rfidProto = grpc.loadPackageDefinition(packageDefinition).grpcserv;
const rfidProto = require('./rfid.proto');
// Create the gRPC server
function main() {
    var server = new grpc.Server();
    //servis oluşturma
    server.addService(rfidProto.RFIDService.service, { readTag: RFIDService.prototype.readTag });
    
    //gRPC bağlantısı oluşturma
    server.bindAsync("127.0.0.1:5000", grpc.ServerCredentials.createInsecure(), (err) => {
      server.start();
      console.log("GRPC Server 5000 üzerinde başlatıldı.....");
    });
  }
  main();

下面是错误:

package rfidgrpc;
        ^^^^^^^^

SyntaxError: Unexpected identifier
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1049:15)
    at Module._compile (node:internal/modules/cjs/loader:1084:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
    at Module.load (node:internal/modules/cjs/loader:998:32)
    at Module._load (node:internal/modules/cjs/loader:839:12)
    at Module.require (node:internal/modules/cjs/loader:1022:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Users\Nur Seda\Desktop\rfid_grpc\server.js:20:19)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)

我检查了package.json文件。软件包名称之间没有区别。我没有使用任何特殊字符。

hgb9j2n6

hgb9j2n61#

试试这个:

const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');

var PROTO_PATH = `${__dirname}/rfid.proto`;

var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {
      // Perhaps this should be false?
      keepCase: true,
      longs: String,
      enums: String,
      defaults: true,
      oneofs: true
    });

// Package name is appended here
const rfidProto = grpc.loadPackageDefinition(packageDefinition).rfidgrpc;

// Implement method
// Not camelCased here
function readRFID(call, callback) {
  callback(null, {
    tag: "value"
  });
}
function main() {
    var server = new grpc.Server();

    // By the documentation that this should be readRFID:readRFID
    // But, when testing, I needed to reflect the Proto's Method Name
    server.addService(rfidProto.RFIDService.service, { ReadRFID: readRFID });

    server.bindAsync(
        "127.0.0.1:5000",
        grpc.ServerCredentials.createInsecure(), (err) => {
            server.start();
            console.log("GRPC Server 5000 üzerinde başlatıldı.....");
        }
    );
 }

main();

使用gRPCurl测试:

grpcurl \
-plaintext \
--import-path=${PWD} \
-proto ${PWD}/rfid.proto \
localhost:5000 \
list
rfidgrpc.RFIDService

grpcurl ... \
list rfidgrpc.RFIDService
rfidgrpc.RFIDService.ReadRFID

grpcurl ... \
rfidgrpc.RFIDService.ReadRFID
{
  "tag": "value"
}

相关问题