feat: add minimal json-rpc node example

Added examples/jsonrpc-get-block.js, demonstrating how to access geth RPC via JSON-RPC using only standard Node.js modules.
This commit is contained in:
GeneratedUserFJ839 2025-12-15 18:39:28 +01:00 committed by GitHub
parent 6978ab48aa
commit 3d1ced53b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,60 @@
#!/usr/bin/env node
/**
* Minimal JSON-RPC example that fetches the latest block number
* from a running geth node using only Node.js built-ins.
*
* Usage:
* node examples/jsonrpc-get-block.js
*
* Optionally override RPC URL:
* RPC_URL=http://127.0.0.1:8545 node examples/jsonrpc-get-block.js
*/
const http = require("http");
const rpcUrl = process.env.RPC_URL || "http://127.0.0.1:8545";
const { hostname, port, pathname } = new URL(rpcUrl);
const payload = JSON.stringify({
jsonrpc: "2.0",
method: "eth_blockNumber",
params: [],
id: 1,
});
const options = {
hostname,
port,
path: pathname || "/",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(payload),
},
};
const req = http.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
try {
const json = JSON.parse(data);
console.log("RPC response:", json);
} catch (e) {
console.error("Failed to parse response:", e);
console.error("Raw response:", data);
}
});
});
req.on("error", (err) => {
console.error("Request error:", err);
});
req.write(payload);
req.end();