2

Server is throwing this error, when client-side app is trying to send a POST request with base64-encoded image.

Error
    at readStream (/Users/.../node_modules/raw-body/index.js:196:17)
    at getRawBody (/Users/.../node_modules/raw-body/index.js:106:12)
    at read (/Users/.../node_modules/body-parser/lib/read.js:76:3)
    at jsonParser (/Users/.../node_modules/body-parser/lib/types/json.js:127:5)
    at Layer.handle [as handle_request] (/Users/.../node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/.../node_modules/express/lib/router/index.js:317:13)
    at /Users/.../node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/.../node_modules/express/lib/router/index.js:335:12)
    at next (/Users/.../node_modules/express/lib/router/index.js:275:10)
    at cors (/Users/.../node_modules/cors/lib/index.js:179:7)
    at /Users/.../node_modules/cors/lib/index.js:229:17
    at originCallback (/Users/.../node_modules/cors/lib/index.js:218:15)
    at /Users/.../node_modules/cors/lib/index.js:223:13
    at optionsCallback (/Users/.../node_modules/cors/lib/index.js:204:9)
    at corsMiddleware (/Users/.../node_modules/cors/lib/index.js:209:7)
    at Layer.handle [as handle_request] (/Users/.../node_modules/express/lib/router/layer.js:95:5)

I've doublechecked the request body: base64 string is not malformed. If app is sending the same request without base64 part, everything goes ok.

server.js:

import http from 'http'
import express from 'express'
import cors from 'cors'
import morgan from 'morgan'
import bodyParser from 'body-parser'
import api from './api'
import config from './config'

let app = express()
app.server = http.createServer(app)

// logger
app.use(morgan('dev'))

// 3rd party middleware
app.use(cors({
    exposedHeaders: config.corsHeaders
}))

app.use(bodyParser.json({
    limit : config.bodyLimit
}))

app.use(express.static("./assets"))

// api router
app.use('/api', api)

app.server.listen(process.env.PORT || config.port)

console.log(`Started on port ${app.server.address().port}`)

export default app

api.js

import { Router } from 'express';
const router = Router();

router.post('/createPost', require('./methods/createPost').default)

export default router
stkvtflw
  • 9,002
  • 19
  • 50
  • 110
  • FWIW I'm having the exact same issue. If I post a request with a trivially small test body, it goes through successfully, but if the body of my request is larger (audio-video data), the error above is displayed. – TonyM Apr 15 '17 at 23:37

1 Answers1

5

I think it's the same as this issue:

Error: request entity too large

Body parser's limit is set too low (the default is 100k).

In my case, I found that setting limit: '50mb' as described in the solution in the link above fixed my issue:

app.use(bodyParser.json({limit: '50mb'}));

It looks like your limit is being set in config.

Community
  • 1
  • 1
TonyM
  • 174
  • 2
  • 10