app.use(async ctx => {
console.log("ctx",ctx)
if ("/qsonhs.aspx" == ctx.path) {
http
.get("http://nodejs.cn/index.json", res => {
const { statusCode } = res;
const contentType = res.headers["content-type"];
let error;
if (statusCode !== 200) {
error = new Error("\n" + `: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error(
" content-type.\n" +
` application/json ${contentType}`
);
}
if (error) {
console.error(error.message);
//
res.resume();
return;
}
res.setEncoding("utf8");
let rawData = "";
res.on("data", chunk => {
rawData += chunk;
});
res.on("end", () => {
try {
const parsedData = JSON.parse(rawData);
console.log(parsedData);
} catch (e) {
console.error(e.message);
}
});
})
.on("error", e => {
console.error(`: ${e.message}`);
});
}
});