我收到以下错误与快递:

Error: request entity too large
    at module.exports (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/node_modules/raw-body/index.js:16:15)
    at json (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/json.js:60:5)
    at Object.bodyParser [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:53:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.cookieParser [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:60:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.logger (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/logger.js:158:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.staticMiddleware [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/static.js:55:61)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
TypeError: /Users/michaeljames/Documents/Projects/Proj/mean/app/views/includes/foot.jade:31
    29| script(type="text/javascript", src="/js/socketio/connect.js")
    30| 
  > 31| if (req.host='localhost')
    32|     //Livereload script rendered 
    33|     script(type='text/javascript', src='http://localhost:35729/livereload.js')  
    34| 

Cannot set property 'host' of undefined
    at eval (eval at <anonymous> (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:152:8), <anonymous>:273:15)
    at /Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:153:35
    at Object.exports.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:197:10)
    at Object.exports.renderFile (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:233:18)
    at View.exports.renderFile [as engine] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:218:21)
    at View.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/view.js:76:8)
    at Function.app.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/application.js:504:10)
    at ServerResponse.res.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/response.js:801:7)
    at Object.handle (/Users/michaeljames/Documents/Projects/Proj/mean/config/express.js:82:29)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:188:17)

POST /api/0.1/people 500 618ms

我用的是meanstack。我有以下使用语句在我的express.js

//Set Request Size Limit
app.use(express.limit(100000000));

在fiddler中,我可以看到内容长度头的值为:1078702

我想这是八位字节,这是1.0787兆字节。

我不知道为什么express不让我发布json数组,我之前在另一个express项目中发布,该项目没有使用平均堆栈项目结构。


当前回答

我不认为这是快速全球大小限制,但特别是连接。Json中间件限制。当您使用express.bodyParser()并且不提供限制选项时,默认为1MB。

Try:

app.post('/api/0.1/people', express.bodyParser({limit: '5mb'}), yourHandler);

其他回答

只要加上这一行就能解决问题

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

还建议大家将整个图像发送到后端,然后转换它,而不是从前端发送数据

下面的设置对我很有效

表达4.16.1

app.use(bodyParser.json({ limit: '50mb' }))
app.use(bodyParser.urlencoded({
  limit: '50mb',
  extended: false,
}))

恩金克斯

client_max_body_size 50m;
client_body_temp_path /data/temp;

我不认为这是快速全球大小限制,但特别是连接。Json中间件限制。当您使用express.bodyParser()并且不提供限制选项时,默认为1MB。

Try:

app.post('/api/0.1/people', express.bodyParser({limit: '5mb'}), yourHandler);

在我的例子中,从请求头中删除Content-type是有效的。

补充一下亚历山大的答案。

默认情况下,NGINX对每个文件的上传限制为1mb。通过限制上传的文件大小,可以防止某些类型的拒绝服务(DOS)攻击和许多其他问题。

因此,当您尝试上传超过1MB限制的文件时,您将遇到413错误。

通过编辑“client_max_body_size”,可以调整上传文件的大小。使用http、server或location块编辑client_max_body_size。

server {
  server_name example.com;

  location / {
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_pass http://127.0.0.1:8080;
    client_max_body_size 20M;
  }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/infohob.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/infohob.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

参考:在NGINX限制文件上传大小