server.ts
1 import Koa from "koa" 2 import Router from "@koa/router"; 3 import bodyParser from "@koa/bodyparser" 4 import { ipfs_add, ipfs_cat } from "./main"; 5 6 import cors from "@koa/cors"; 7 8 const app = new Koa(); 9 const router = new Router(); 10 11 router.get('/ipfs-get/:cid', async (ctx, next) => { 12 const { cid } = ctx.params 13 14 const content = await ipfs_cat(cid) 15 console.log({got: content, cid}) 16 return ctx.body = content 17 }) 18 19 router.post('/ipfs-add/', async (ctx, next) => { 20 const body = ctx.request.body 21 22 const cid = await ipfs_add(JSON.stringify(body)) 23 24 console.log({ipfs_add: {cid, body}}) 25 ctx.body = {cid} 26 27 return 28 }) 29 30 app 31 .use(cors()) 32 .use(bodyParser()) 33 .use(router.routes()) 34 .use(router.allowedMethods()) 35 36 37 app.listen(3000)