Skip to content

parse.json function not working after calling it in earlier middleware #81

@cleverocheck

Description

@cleverocheck

Hello everyone, I am writing a Koa application in TS and I have one problem with the co-body library. It lies in the fact that during one request the maximum number of normally working calls to the parse.json function = 1 (on the second call to the parse.json function, the request always ends with just gateway timeout). And as a result, I have to make crutches in the form of a call to parse.json on one of the most faithful middleware and transfer it to the bottom through ctx, which is why the typing is not done in the best way, the purity of the code suffers and this is lower in performance (because sometimes I no need to call parse.json at all)

An example of a code showing this problem (the parse.json function call is not included in separate middleware, but there is no difference as such)

const koa = require('koa')
const parse = require('co-body')

const app = new koa()

app.use(async ctx => {
    const bodyOk = await parse.json(ctx.req)
    console.log('Body is ok', bodyOk)
    await parse.json(ctx.req)
    console.log('Will never reach it log...')
})

const PORT = 3000
app.listen(PORT, () => console.log(`API listening on ${PORT}`))

I'm testing this problem through Postman with this content

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions