node --- http数据上传
生活随笔
收集整理的這篇文章主要介紹了
node --- http数据上传
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// 通過報頭的Transfer-Encoding或Content-Length即可判斷請求中是否帶有內容
var hasBody = function(req) {return 'transfer-encoding' in req.headers || 'content-length' in req.headers;
};// 在HTTP_Parser解析報頭結束后,報文內容部分會通過data事件觸發
function (req, res) {if (hasBody(req)) {var buffers = [];req.on('data', function (chunk) {buffers.push(chunk);});req.on('end', function () {req.rawBody = Buffer.concat(buffers).toString();handle(req, res);});} else {handle(req, res);}
}
表單數據:最為常見的數據提交就是通過網頁表單提交數據到服務器端:
<form action="/upload" method="post"><label for="username">Username:</label> <input type="text" name="username" id="username" /><br /><input type="submit" name="submit" value="Submit" /> </form> // 默認的表單提交,請求頭的Content-Type字段值為application/x-www-form-urlencoded // 即:Content-Type: application/x-www-form-urlencoded // 解析 var handle = function(req, res) {if (req.headers['content-type'] === 'application/x-www-form-urlencoded') {req.body = querystring.parse(req.rawBody);}todo(req, res); };其他格式: JSON和XML文件等,判斷和解析他們的原理都比較相似,都是依據Content-Type中的值決定,其中JSON類型的值
為application/json,XML的值為application/xml
總結
以上是生活随笔為你收集整理的node --- http数据上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 7-4 哈利·波特的考试 (25 分)(
- 下一篇: python打开文件要wordcloud