代码拉取完成,页面将自动刷新
const fs = require('fs');
const path = require('path');
const https = require('https');
const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
const location = {
host: "www.baidu.com",
hostname: "www.baidu.com",
href: "https://www.baidu.com/",
origin: "https://www.baidu.com",
pathname: "/",
port: "",
protocol: "https:",
}
async function proxySite(ctx) {
// console.log('\n --------------------- \n');
// console.log(ctx.request, '\n ======== \n');
return new Promise(resolve => {
https.get(location.href, {
headers: {
...ctx.request.header,
host: location.host,
},
}, res => {
if (res.statusCode !== 200) {
resolve([res.statusCode]);
return;
}
let data = '';
// res.setEncoding('utf-8');
res.on('data', chunk => {
data += chunk.toString();
console.log(chunk.toString());
});
res.on('end', () => resolve([null, data]));
}).on('error', err => resolve([err]));
});
}
router.get('/', async (ctx, next) => {
const [err, data] = await proxySite(ctx);
fs.writeFileSync(path.join(__dirname, 'test.html'), data);
// ctx.body = err || data;
next();
});
app.use(router.routes());
app.listen(4000, () => console.log(`Server started ... ${4000}`));
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。