let http = require('http'); let util = require('util'); http.get('http://www.hao123.com/sugdata_s4.json?r=-838709', (res) => { const { statusCode } = res; const contentType = res.headers['content-type']; console.log(statusCode); console.log(contentType); let error; if (statusCode !== 200) { error = new Error('请求失败。 ' + `状态码: ${statusCode}`); } else if (!/^application/json/.test(contentType)) { error = new Error('无效的 content-type. ' + `期望 application/json 但获取的是 ${contentType}`); } let data = ''; res.on("data", function (chunk) { data += chunk; }); res.on("end", function () { let result = JSON.parse(data); console.log("result:"+ util.inspect(result)); }); });