Ai
1 Star 1 Fork 1

草鞋没号/test-http-proxy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
proxy.js 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
草鞋没号 提交于 2020-12-08 15:39 +08:00 . first commit
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}`));
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cxmh/test-http-proxy.git
git@gitee.com:cxmh/test-http-proxy.git
cxmh
test-http-proxy
test-http-proxy
master

搜索帮助