无法运行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:
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
pathvalue: Thepathshould be the specific endpoint path on your RPC server (like/jsonrpc), nothttps://. If your server uses HTTPS, userpc.ClientSecureinstead ofrpc.Client, or add aprotocol: 'https'parameter to the options object. - Parameter validation: Double-check that your server’s
Pingmethod actually accepts aclassnameparameter—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
Pingmethod is coded to return a "Pong" response when given theclassname: 'NServer'parameter - If using HTTPS or firewall rules, ensure there’s no connection block between your client and server
内容的提问来源于stack exchange,提问作者Neha C

