Nodejs:在http.request的浏览器上打印结果

时间:2014-07-01 09:35:01

标签: node.js

我试图在浏览器上打印http.request的结果,但我不能。通常我可以使用res.end("text on the browser")执行此操作,但在此处不起作用。

var http = require("http");

var options = {
    host: '127.0.0.1',
    port: 8080,
    path: '/',
    method: 'POST'
};

var req = http.request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
            res.end(chunk); //I WANT TO PRINT THIS ON BROWSER
    });

});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

req.write('data\n');
req.end();

我有这个错误:

res.end(chunk);
TypeError: Object #<IncomingMessage> has no method 'end'

我该怎么办?请给我一个解决这个问题的补救措施..

0 个答案:

没有答案