Saturday, 15 April 2017

Express: Error at readStream

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



via stkvtflw

No comments:

Post a Comment