[英]How to increase the limit of the Node.js querystring post request without Express?
我將數據從瀏覽器傳遞到我的Node.js應用程序,並使用以下代碼將數據寫入文件:
的index.html
<style>
iframe{display:none;}
</style>
<form method="post" target="iframe">
<textarea name="my_textarea">Some code with tags e.g. <p>text</p></textarea>
</form>
<button>SEND</button>
<iframe name="iframe"></iframe>
<script>
document.getElementsByTagName('button')[0].addEventListener('click', function() {
document.getElementsByTagName('form')[0].submit();
});
</script>
Node.js應用程序
var http = require('http'),
fs = require('fs'),
querystring = require('querystring');
http.createServer(function(req, res) {
fs.readFile(__dirname+'/index.html', function(err, indexPage) {
if(!err) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(indexPage);
}
});
if(req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var post = querystring.parse(body);
if(post['my_textarea']) {
var anotherPage = post['my_textarea'];
fs.writeFile(__dirname+'/another.html', anotherPage, function(err) {
if(err) {
return console.log(err);
}
});
}
});
}
}).listen(80, 'localhost');
經過測試,我發現這樣的請求通過了不超過544 000個字符
有可能指定某處或某種方式我自己的限制,如何?
我知道Express可以這樣做
app.use(express.bodyParser({limit: '50mb'}));
但是我很感興趣如何在沒有Express的情況下做到這一點
PS。 我在本地機器上發送自己的帖子請求,據我所知,我不需要類似的東西
...
body += data;
if(body.length > 1e6) {
request.connection.destroy();
}
...
但如果我還需要它,請解釋原因
也許這樣的事情,但這是1 Mb我認為。 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
req.on('data', function (data) {
body += data;
if (body.length > 1e6)
req.connection.destroy();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.