diff --git a/package.json b/package.json index deaf4c4..61066df 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dateformat": "^3.0.2", "isomorphic-fetch": "^2.2.1", "koa": "^2.6.2", + "koa-bodyparser": "^4.2.1", "koa-cors": "0.0.16", "koa-mount": "^4.0.0", "koa-router": "^7.4.0", diff --git a/server/index.js b/server/index.js index 1aac349..482edaa 100644 --- a/server/index.js +++ b/server/index.js @@ -3,6 +3,7 @@ const Router = require('koa-router'); const Static = require('koa-static'); const Cors = require('koa-cors'); const Mount = require('koa-mount'); +const BodyParser = require('koa-bodyparser'); const path = require('path'); const routePictures = require('./route/pictures'); @@ -20,6 +21,7 @@ routeCBeamRPC(router); app .use(Cors()) + .use(BodyParser()) .use(router.routes()) .use(router.allowedMethods()) .use(Static(path.resolve(__dirname, '../dist'), {})) diff --git a/server/route/cbeamRpc.js b/server/route/cbeamRpc.js index d9e759c..3879bb8 100644 --- a/server/route/cbeamRpc.js +++ b/server/route/cbeamRpc.js @@ -1,16 +1,33 @@ require('isomorphic-fetch'); +const rpcRequest = async (method, params = {}) => { + const body = JSON.stringify({ + jsonrpc: '2.0', + id: +new Date(), + method, + params, + }); + const res = await fetch('http://c-beam.cbrp3.c-base.org:4254/rpc/', { + headers: { + 'content-type': 'application/json', + }, + mode: 'cors', + credentials: 'omit', + method: 'POST', + body, + }); + return res; +}; + module.exports = (router) => { router.get('/rpc/:method', async (ctx) => { - const res = await fetch('http://c-beam.cbrp3.c-base.org:4254/rpc/', { - credentials: 'omit', - headers: { - 'content-type': 'application/json', - }, - body: `{"jsonrpc":"2.0","id":1550009052773,"method":"${ctx.params.method}","params":""}`, - method: 'POST', - mode: 'cors', - }); + const res = await rpcRequest(ctx.params.method); + ctx.assert((res.status === 200), res.status); + const body = await res.json(); + ctx.body = body; + }); + router.post('/rpc/:method', async (ctx) => { + const res = await rpcRequest(ctx.params.method, ctx.request.body); ctx.assert((res.status === 200), res.status); const body = await res.json(); ctx.body = body;