Blogger Information
Blog 143
fans 1
comment 0
visits 443138
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
配置express上传文件大小
弘德誉曦的博客
Original
2357 people have browsed it

Node.JS HTTP请求上传参数最大限制修改

2018年04月21日 21:04:44 zding92 阅读数:1007

 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011481543/article/details/80033546

Node.JS HTTP请求上传参数最大限制修改

在HTTP请求时,POST PUT方法理论上,没有参数大小限制。但实际中在服务器端,会限制HTTP请求的大小。所以会出现带大参数的请求服务器无法响应的情况。 
特别是在使用富文本编辑器图片采用Base64编码的情况下,默认的1MB的请求参数大小很容易超过,因此,需要修改HTTP请求的大小限制 
在Node.JS中,在bodyparser中修改这个限制即可:

app.use(bodyParser.json({limit:'100mb'}));app.use(bodyParser.urlencoded({ limit:'100mb', extended: true }));12

此处,将限制大小控制在100MB。这样,就可以一定程度上避免大参数请求服务器无法响应的情况。 
另外,附上整个中间件的顺序:

var app = express();// view engine setupapp.set('views', path.join(__dirname, '/src/views'));
app.engine('html', require('ejs').__express);  
app.set('view engine', 'html');// app.set('view engine', 'jade');// uncomment after placing your favicon in /public//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));//----------------------------- Body Parser & Cookie -------------------------------------app.use(logger('dev'));
app.use(bodyParser.json({limit:'100mb'}));
app.use(bodyParser.urlencoded({ limit:'100mb', extended: true }));
app.use(cookieParser());//----------------------------- 静态资源 ------------------------------------app.use(express.static(path.join(__dirname, 'public/dist')));//----------------------------- 跨域处理-------------------------------------app.all('*', function(req, res, next) {  
 res.header("Access-Control-Allow-Origin", "*");  
 res.header("Access-Control-Allow-Headers", "X-Requested-With,xtoken");  
 res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");  
 res.header("X-Powered-By",' 3.2.1')  
 res.header("Content-Type", "application/json;charset=utf-8");  
 next();  
});

//---------------------------- Midware ------------------------------------app.use('/', authMidware);//---------------------------- Router --------------------------------------app.use('/', routes);
app.use('/view', view);
app.use('/admin', admin);


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post