stream-utils / raw-body

Get and validate the raw body of a readable stream

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: stream.on is not a function

dawadam opened this issue · comments

Hello,
I have this error, I am using Koa middleware.

TypeError: stream.on is not a function
    at readStream (/node_modules/raw-body/index.js:197:10)
    at executor (/node_modules/raw-body/index.js:113:5)
    at new Promise (<anonymous>)
    at getRawBody (/node_modules/raw-body/index.js:112:10)
    at /src/index.ts:99:30
    at step (/src/index.ts:33:23)
    at Object.next (/src/index.ts:14:53)
    at /src/index.ts:8:71
    at new Promise (<anonymous>)
    at __awaiter (/src/index.ts:4:12)
    at /src/index.ts:95:29
    at dispatch (/node_modules/koa-compose/index.js:42:32)
    at filter (/node_modules/koa-json/index.js:25:12)
    at dispatch (/node_modules/koa-compose/index.js:42:32)
    at cors (/node_modules/@koa/cors/index.js:59:38)
    at dispatch (/node_modules/koa-compose/index.js:42:32)

My code is :

var contentType = require('content-type')
var getRawBody = require('raw-body')

    app.use(async (ctx, next) => {

        if (ctx.request.originalUrl.startsWith('/raw-parse')) {

            ctx.text = await getRawBody(ctx.request, {
                length: ctx.request.headers['content-length'],
                limit: '1mb',
                encoding: contentType.parse(ctx.request).parameters.charset
            })
        }

        await next();
    })

Hello, and sorry for that problem. It looks like the argument you are passing into this module, ctx.request is not a stream. We can improve the error message this module gives instead of assuming a stream was passed 👍

Ok I see.
I don't know how to retrieve the Koa request stream.
But you put an example with Koa in the Readme, does it work? Because I don't understand it.

Edit:
I think i'm juste need to change ctx.request by ctx.req
Thank you for the quick reply