nodejs 实现简单 http 代理并缓存
var http = require('http'),
fs = require("fs"),
url = require('url'),
querystring = require('querystring')
function onRequest(req, res) {
var reqUrl = req.url
if ('/' == reqUrl || 'favicon.ico' == reqUrl) {
return res.end('Im debugger')
}
var proxySet = req.headers.ps
console.log(proxySet)
if (proxySet) proxySet = JSON.parse(proxySet)
proxySet = proxySet || {}
delete req.headers.ps
res.writeHead( ,{'Content-Type': 'text/plain','Cache-Control': 'no-cache,no-store'})
if (proxySet.cache){
var cacheFile = './cache/'+ proxySet.cache
if (!fs.existsSync(cacheFile)) fs.mkdirSync(cacheFile)
cacheFile += '/'+ reqUrl.replace(/^\//,'').replace(/\//g,'-')
if (fs.existsSync(cacheFile)){
console.log('from cache ' , cacheFile)
return fs.createReadStream(cacheFile).pipe(res)
}
}
var source = 'target.com'
delete req.headers['accept-encoding']
var httpProxy = getProxy(source , req ,res ,cacheFile)
req.pipe(httpProxy)
}
function getProxy(host , req ,res , cacheFile){
var backTimeoutTTL =
req.headers.host = host
var options = {
host : host,
port : ,
headers: req.headers,
path : req.url,
agent : false,
method : req.method ,
};
var request_timer;
var httpProxy = http.request(options , function(response) {
if (request_timer) clearTimeout(request_timer)
response.setEncoding('utf8')
response.pipe(res)
cacheFile && response.pipe(fs.createWriteStream(cacheFile))
});
httpProxy.on('error' , function(e){
res.end('error happend :' + req.url)
})
request_timer = setTimeout(function() {
console.log('request timeout [%s] %s' , host , req.url)
httpProxy.abort();
res.end('request timeout :' + req.url)
}, backTimeoutTTL);
return httpProxy
}
http.createServer(onRequest).listen()
nodejs 实现简单 http 代理并缓存的更多相关文章
- 6、nginx的反向代理及缓存功能
nginx模块的应用 ngx_http_proxy_module nginx 反向代理模块: http://nginx.org/en/docs/http/ngx_http_proxy_module. ...
- squid代理与缓存(下)
squid代理与缓存(下) 6. squid代理模式案例 6.1 squid传统正向代理生产使用案例 6.1.1 squid传统正向代理两种方案 (1)普通代理服务器 作为代理服务器,这是SQUID的 ...
- squid代理与缓存(上)
squid代理与缓存(上) 1. Squid介绍 1.1 缓存服务器介绍 缓存服务器(英文意思cache server),即用来存储(介质为内存及硬盘)用户访问的网页,图片,文件等等信息的专用服务器. ...
- Apache服务器代理与缓存
Apache服务器代理与缓存 1.代理 正向代理: 反向代理: mod_proxy模块 apache实现代理和网关的关键模块.加载mod_proxy模块:LoadModu ...
- nginx简单反向代理和负载均衡(ubuntu)
nginx简单反向代理与负载均衡 环境:三台ubuntu 12.04.5 虚拟机 均装有nginx 1.1.19 以下u1(192.168.240.129) ,u2(192.168.240.13 ...
- nodejs的简单爬虫
闲聊 好久没写博客了,前几天小颖在朋友的博客里看到了用nodejs的简单爬虫.所以小颖就自己试着做了个爬博客园数据的demo.嘻嘻...... 小颖最近养了条泰日天,自从养了我家 ...
- 学习笔记:Nginx反射代理使用缓存和删除其缓存文件的方法
使用nginx做cache服务器 需求就是缓存android的软件包,后缀名是apk.话不多说,直接上配置,供参考: a-->nginx.conf user www www; worker_pr ...
- 利用socket.io+nodejs打造简单聊天室
代码地址如下:http://www.demodashi.com/demo/11579.html 界面展示: 首先展示demo的结果界面,只是简单消息的发送和接收,包括发送文字和发送图片. ws说明: ...
- Nginx 反向代理并缓存及缓存清除
Nginx 反向代理并缓存及缓存清除 原文地址:http://www.cnblogs.com/caoguo/p/5012447.html 一. Nginx 配置 #user nobody; worke ...
随机推荐
- 8:30+1.5小时,返回时间格式的 php函数
一个实用的自定义函数 /** * 传入8:30格式的开始时间,和小数形式的小时长度,返回结束时间 * @param [type] $start [description] 8:30 * @param ...
- 纯文本中识别URI地址并转换成HTML
问题 有一段纯文本text, 欲将其插入DOM节点div中. text中可能有超链接, 邮件地址等. 假设有, 识别之. 分析 假设仅仅是纯文本, 插入div中, 仅仅要将div.innerText设 ...
- HDU 5305 Friends (搜索+剪枝) 2015多校联合第二场
開始对点搜索,直接写乱了.想了想对边搜索,尽管复杂度高.剪枝一下水过去了. 代码: #include<cstdio> #include<iostream> #include&l ...
- NYOJ 915 +-字符串【贪心】
+-字符串 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描写叙述 Shiva得到了两个仅仅有加号和减号的字符串,字串长度同样.Shiva一次能够把一个加号和它相邻的减号交换 ...
- ADO.NET (二)—— ADO和ADO .NET对照
ADO.NET (二)-- ADO和ADO .NET对照 我们知道ADO.NET的两大核心组件各自是Data Provider和DataSet.假设说 DataSet是ADO.NET的心 ...
- Double 与 Float 的值的比較结果
首先看geeksforgeeks上的两个程序: 程序1: #include<stdio.h> int main() { float x = 0.1; if (x == 0.1) print ...
- hpc-ai比赛相关资料
http://follitude.com/blog/Use-RDMA-Emulation-in-Software-Using-SoftiWARP/ https://www.reflectionsoft ...
- xBIM 基础15 IFC导出Excel报表
系列目录 [已更新最新开发文章,点击查看详细] IFC导出Excel空间报表文件 本篇将向您展示从IFC文件读取数据所需的一些概念.它使用IFC4接口,适用于IFC2x3和IFC4型号.要创建 ...
- 路飞学城Python-Day10(practise)
作业:现要求你写一个简单的员工信息增删该查程序,需求如下:当然此表在文件存储时可以这样表示1,Alex Li,22,13651054608,IT,2013-04-012,Jack Wang,28,13 ...
- 多任务-进程之进程池Pool
1.什么是池? 首先从字面上看,池代表着一个容器,用来承载着某些内容的容器,了解到这里,就对进程池有了一个初步的轮廓. 2.什么是进程池Pool? (1)利用现实中的事物来理解: 对于小白初学者,接触 ...