I wanted to use koa & koa-bodyparser with TypeScript but whenever I access ctx.request.body
I get an error that body
doesn't exist on type Request
import Koa from 'koa'
import Router from 'koa-router'
import bodyparser from 'koa-bodyparser'
const app = new Koa()
const router = new Router()
const data = ['lorem', 'ipsum', 'dolor', 'sit', 'amet']
app.use(bodyparser())
router.post('/', (ctx, next) => {
const phrase = ctx.request.body; // Property 'body' does not exist on type Request
if (typeof phrase === 'string') {
ctx.response.body = data.filter(element => element.includes(phrase))
}
})
Run npm install --save-dev @types/koa-bodyparser
in a terminal while in the directory where your package.json is
This package contains types introduced by koa-bodyparser (such as request.body)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With