You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法运行JSON RPC测试用例自动化的Node.js脚本求助

Hey, let's work through your issues step by step—first fixing that npm command error, then getting your RPC script to work as expected:

Fixing Your Node.js JSON RPC Test Script Errors

1. Resolve the npm run Command Issue

That missing script error pops up because npm run is designed to execute pre-defined scripts in your package.json, not run individual JavaScript files directly.

To launch your test script right away, use the node command instead:

node test/resources/nv-jsonrpc-tests/pingServer.spec.js

If you’re using a test framework like Jest or Mocha, first add a test script to your package.json:

{
  "scripts": {
    "test": "jest"
  }
}

Then run the single test file with:

npm test test/resources/nv-jsonrpc-tests/pingServer.spec.js

(Note: For Mocha, add a space and -- before the file path: npm test -- test/resources/...)

2. Fix the RPC Client Script Logic

Looking at your code, there are a couple of critical issues that will block you from getting that expected "Pong" response:

  • Incorrect path value: The path should be the specific endpoint path on your RPC server (like /jsonrpc), not https://. If your server uses HTTPS, use rpc.ClientSecure instead of rpc.Client, or add a protocol: 'https' parameter to the options object.
  • Parameter validation: Double-check that your server’s Ping method actually accepts a classname parameter—mismatched or unrecognized params will cause the request to fail.

Here’s the corrected script:

var rpc = require('node-json-rpc');

// Use rpc.ClientSecure instead if your server uses HTTPS
var options = {
  port: 8080,
  host: '127.0.0.1',
  // Update this to your actual RPC endpoint path
  path: '/jsonrpc',
  strict: true,
  // Uncomment this line if using an HTTPS server
  // protocol: 'https',
};

var client = new rpc.Client(options);

client.call(
  {
    "jsonrpc": "2.0",
    "method": "Ping",
    "params": {classname: 'NServer'},
    "id": 1
  },
  function (err, res) {
    if (err) {
      console.error('RPC Request Failed:', err);
    } else {
      console.log('RPC Response:', res);
    }
  }
);

3. Quick Troubleshooting Checks

  • Confirm your RPC server is running and actively listening on port 8080
  • Verify the server’s Ping method is coded to return a "Pong" response when given the classname: 'NServer' parameter
  • If using HTTPS or firewall rules, ensure there’s no connection block between your client and server

内容的提问来源于stack exchange,提问作者Neha C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:57:27