跳到主要内容
CNode

POST 数据不完整

社区
Uuseless发布于 13 年前最后回复 13 年前
242990

代码很简单: 获取 Post 数据. 用在配合 gitlab webhook 的,但发现经常有获取的数据不完整的情况出现, 抓包证实到这台机器的数据是完整的. 不知道有没有朋友遇到这种情况没..


var http = require('http');
http.createServer(function (req, res) {
    var postData = '';

    req.setEncoding('utf8');
    req.addListener('data', function(postDataChunk) {
        postData += postDataChunk;
    });

    req.addListener('end', function() {
        // handle(JSON.parse(postData));
        console.log(postData);
    });

    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end("done\n");
}).listen(8080, '0.0.0.0');

node0.10.15 rh5

查看回复

回复 (2)

U
useless#113 年前

原来是 end() 早了. 这样就 ok 了..


    req.addListener('end', function() {
        // handle(JSON.parse(postData));
        console.log(postData);

        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end("done\n");
    });

    // res.writeHead(200, {'Content-Type': 'text/plain'});
    // res.end("done\n");
}).listen(8080, '0.0.0.0');

参与回复

登录后即可参与回复。登录