Ai
1 Star 1 Fork 1

草鞋没号/test-http-proxy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
express.js 1.44 KB
一键复制 编辑 原始数据 按行查看 历史
草鞋没号 提交于 2020-12-08 15:39 +08:00 . first commit
const path = require('path');
const fs = require('fs');
const express = require('express');
const https = require('https');
const app = express();
const location = {
hash: "",
host: "www.jianshu.com",
hostname: "www.jianshu.com",
href: "https://www.jianshu.com/",
origin: "https://www.jianshu.com",
pathname: "/",
port: "",
protocol: "https:",
};
function proxy(req, res, next) {
return new Promise(resolve => {
const handle = https.get(location.origin, {
host: location.host,
}, response => {
if (response.statusCode !== 200) {
console.log('statusCode:', response.statusCode);
resolve([response.statusCode]);
return;
}
let data = '';
response.setEncoding('utf-8');
response.on('data', chunk => {
data += chunk.toString();
})
response.on('end', () => {
// console.log('data:\n', data);
resolve([null, data]);
})
});
handle.on('error', err => {
resolve([err]);
})
});
}
app.get('/', async (req, res, next) => {
const [err, data] = await proxy(req, res, next);
if (err) {
console.log('Error:\n ', err);
return;
}
fs.writeFileSync(path.join(__dirname, './result.html'), data);
const script = fs.readFileSync(path.join(__dirname, 'extra.js'), 'utf-8');
res.end(data.replace('</body>', `<script>${script}</script></body>`));
next();
})
app.listen(3000, () => { console.log(`Server run at 3000 port.`) });
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

搜索帮助