node.js - Nodejs : response. write() for https.request?

标签 node.js

您好,我正在尝试向 API 服务器发出 https.request 。我可以接收该 block 并将其打印在控制台中。如何直接写入html并在浏览器中显示?

我试图寻找与 http.requestresponse.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/

相关文章:

node.js - 点击 Google Vision Api 时出现错误

javascript - Cheerio Web Scape : No Line Break Between Tags

javascript - Node ,如何在 Node/express 应用程序上发送响应 header ?

node.js - 程序列表中的 Electron 生成器应用程序名称没有 Windows 版本

node.js - 保存时检查 Node.js 代码

javascript - 如果应用程序已经在监听,如何添加快速路线?

node.js - Windows 7 中的错误 fsevents@1.0.14

node.js - Node https-代理路径重写/路由器

node.js - 如何使用 Node.js 通过 ffmpeg 流式传输 MP4 文件?

javascript - 为什么 socket.io 为一个事件返回多次