Reading content from URL with Node.js

前端 未结 4 1204
情深已故
情深已故 2020-12-02 20:12

I\'m trying to read the content from a URL with Node.js but all I seem to get are a bunch of bytes. I\'m obviously doing something wrong but I\'m not sure what. This is the

4条回答
  •  悲&欢浪女
    2020-12-02 21:15

    try using the on error event of the client to find the issue.

    var http = require('http');
    
    var options = {
        host: 'google.com',
        path: '/'
    }
    var request = http.request(options, function (res) {
        var data = '';
        res.on('data', function (chunk) {
            data += chunk;
        });
        res.on('end', function () {
            console.log(data);
    
        });
    });
    request.on('error', function (e) {
        console.log(e.message);
    });
    request.end();
    

提交回复
热议问题