JSONRPCErrorException: Request failed with status code 404
constneo opened this issue · 1 comments
constneo commented
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.
shogowada commented
It looks like an express/HTTP request error, unrelated to this library?