shogowada / json-rpc-2.0

Let your client and server talk over function calls under JSON-RPC 2.0 spec. Strongly typed. No external dependencies.

Home Page:https://www.npmjs.com/package/json-rpc-2.0

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSONRPCErrorException: Request failed with status code 404

constneo opened this issue · comments

JSONRPCErrorException: Request failed with status code 404

// server.js
import { JSONRPCServer } from 'json-rpc-2.0'
import Koa from 'koa'
import Router from 'koa-router'
import serve from 'koa-static'
import bodyParser from 'koa-bodyparser'

const app = new Koa()
const router = new Router()
const server = new JSONRPCServer()



server.addMethod('echo', text => {
  console.log('text::: ', text)
  return text
})

server.addMethod('log', ({ message }) => {
  console.log('message::: ', message)
})

router.post('/json-rpc', ctx => {
  /**@type {any}*/
  const body = ctx.request.body
  console.log('ctx.request.body::: ', body)

  server
    .receive(body)
    .then(response => {
      console.log('response::: ', response)
      if (response) {
        ctx.status = 200
        ctx.body = response
      } else {
        ctx.status = 204
      }
    })
    // @ts-ignore
    .catch(err => {
      console.log(err)
    })
})
// Comment out this line and it will cause an error.
// app.use(serve('./public'))
app.use(bodyParser())
app.use(async (ctx, next) => {
  await next()
})
app.use(router.routes())
app.listen(5000)

console.log(`listen:: http://127.0.0.1:5000`)
//client.js
import { JSONRPCClient } from 'json-rpc-2.0'

import axios from 'axios'

// @ts-ignore
const client = new JSONRPCClient(options => {
  console.log('JSONRPCClient options::: ', options)
  const request = axios({
    baseURL: 'http://127.0.0.1:5000',
    url: '/json-rpc',
    method: 'POST',
    headers: {
      'content-type': 'application/json'
    },
    data: options
  }).then(response => {
    if (response.status === 200) {
      client.receive(response.data)
    } else if (options.id !== undefined) {
      return Promise.reject(new Error(response.statusText))
    }
  })

  return request
})

client
  .request('echo', { text: 'Hello, World!' })
  .then(result => {
    console.log('result  2:', result)
  })
  // @ts-ignore
  .catch(err => {
    console.log(err)
  })

client.notify('log', { message: 'Hello, World!' })
# server output
ctx.request.body:::  {
  jsonrpc: '2.0',
  id: 1,
  method: 'echo',
  params: { text: 'Hello, World!' }
}
text:::  { text: 'Hello, World!' }
response:::  { jsonrpc: '2.0', id: 1, result: { text: 'Hello, World!' } }
# client output
JSONRPCClient options:::  {
  jsonrpc: '2.0',
  id: 1,
  method: 'echo',
  params: { text: 'Hello, World!' }
}
JSONRPCErrorException: Request failed with status code 404
    at new JSONRPCErrorException (E:\code\node\koa-demo\node_modules\json-rpc-2.0\dist\models.js:55:28)
    at JSONRPCClient.<anonymous> (E:\code\node\koa-demo\node_modules\json-rpc-2.0\dist\client.js:115:66)
    at step (E:\code\node\koa-demo\node_modules\json-rpc-2.0\dist\client.js:33:23)
    at Object.next (E:\code\node\koa-demo\node_modules\json-rpc-2.0\dist\client.js:14:53)
    at fulfilled (E:\code\node\koa-demo\node_modules\json-rpc-2.0\dist\client.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 0,
  data: undefined
}

Please help me ,What is the reason? thank you.

It looks like an express/HTTP request error, unrelated to this library?