您好,我正在尝试向 API 服务器发出 https.request
。我可以接收该 block 并将其打印在控制台中。如何直接写入html并在浏览器中显示?
我试图寻找与 http.request
的 response.write()
等效的内容,但没有找到。 res.write(chunk)
会给我一个 TypeError
。我怎样才能做到这一点?
var req = https.request(options_places, function(res){
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk){
console.log('BODY: ' + chunk); // Console can show chunk data
res.write(chunk); // This gives TypeError: Object #<IncomingMessage> has no method 'write'
});
});
req.end();
req.on('error', function(e){
console.log('ERROR?: ' + e.message );
});
最佳答案
首先,您必须创建服务器并监听某个端口上的请求。
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Whatever you wish to send \n');
}).listen(3000); // any free port no.
console.log('Server started');
现在它监听 127.0.0.1:3000 处的传入连接
对于特定网址,请使用 .listen(3000,'your url') 而不是 Listen(3000)
关于node.js - Nodejs : response. write() for https.request?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29568455/