Express:readStream出错

时间:2017-04-15 12:37:21

标签: node.js express

当客户端应用尝试使用base64编码的图像发送POST请求时,服务器抛出此错误。

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)

我已经对请求正文进行了双重检查:base64字符串没有格式错误。如果应用程序在没有base64部分的情况下发送相同的请求,一切正常。

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

1 个答案:

答案 0 :(得分:5)

我认为这与此问题相同:

Error: request entity too large

正文解析器的限制设置得太低(默认值为100k)。

在我的情况下,我发现设置限制:' 50mb'如上面链接中的解决方案中所述解决了我的问题:

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

看起来你的限制是在配置中设置的。