繁体   English   中英

Node.js编写http请求响应

[英]Node.js write http request response

我正在使用此代码向我自己的服务器发出HTTP请求。 我正在chunk得到适当的响应。

http.createServer(function (req, res) {
    var options = {
                    host: '<my ip>',
                    port: 8080,
                    method: 'GET',
                    path: '/content?data='+somedata
                };
    var call = http.request(options, function(res){
        res.setEncoding('utf8');                
        res.on('data', function(chunk){
            console.log("got response"+chunk);
        });
    }).on("error", function(e){
        console.log("Got error: " + e.message);
    });
    call.end();
}).listen(3000);

我的问题是如何将这一块打印到浏览器上?

将两个res变量之一更改为一个不同的名称。 目前,对您请求的响应正在掩盖您尝试做出的响应。

然后:

response.write(chunk);

另请参阅: https : //nodejs.org/api/http.html#http_response_write_chunk_encoding_callback

res.write(chunk);

也不要忘记结束首次通话,因此浏览器将知道请求已结束。

res.end("success");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM