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 ...
随机推荐
- js判断 nan null undefined的方法
收集资料如下判断: 1.判断undefined: 复制代码代码如下: <span style="font-size: small;">var tmp = undefin ...
- Linux学习总结(17)——Linux新手必须学会的12个命令
今天的用户可以根据自己的意愿选择是否使用作为Linux象征的命令行,确切的证明了Linux已经有了很大的发展.现在很多Linux发行版的图形用户界面已经非常强大,不再需要命令行. 但是命令行在Linu ...
- java源码之Comparable和Comparator
1,Comparable 简介 Comparable 是排序接口. 若一个类实现了Comparable接口,就意味着“该类支持排序”. 即然实现Comparable接口的类支持排序,假设现在存在“实 ...
- 基于ORA-12170 TNS 连接超时解决办法详解
转自原文 基于ORA-12170 TNS 连接超时解决办法详解 1.开始----程序-----Oracle------配置和移植工具-----Net Manager----本地----服务命名---o ...
- Genymotion出现Installation error: INSTALL_FAILED_CPU_ABI_INCOMPATIBLE错误解决方法
今天在Genymotion上执行曾经的一个项目(libs中有多个SDK和so文件)时,出现下面错误: Console控制台中:Installation error: INSTALL_FAILED_CP ...
- Android 开发之集成百度地图的定位与地图展示
app 应用中,大多数应用都具有定位功能,百度定位就成了开发人员的集成定位功能的首选,近期也在做定位功能,可是发现百度真是个大坑啊, sdk 命名更新了,相关代码却不更新,害得我花费了非常长时间来研究 ...
- Light OJ 1317 Throwing Balls into the Baskets 概率DP
n个人 m个篮子 每一轮每一个人能够选m个篮子中一个扔球 扔中的概率都是p 求k轮后全部篮子里面球数量的期望值 依据全期望公式 进行一轮球数量的期望值为dp[1]*1+dp[2]*2+...+dp[ ...
- cocos2d-x-lua基础系列教程一(hello lua)
myscene.lua function ERROR_TRACBACK(msg) print (==========) print ("lua error is "..tostri ...
- HDFS HA架构以及源代码引导
HA体系架构 相关知识介绍 HDFS master/slave架构,HDFS节点分为NameNode节点和DataNode节点. NameNode存有HDFS的元数据:主要由FSImage和EditL ...
- [jzoj 4528] [GDOI2019模拟2019.3.26] 要换换名字 (最大权闭合子图)
题目链接: https://jzoj.net/senior/#contest/show/2683/0 题目: 题解: 不妨枚举一个点,让两颗树都以这个点为根,求联通块要么点数为$0$,要么包括根(即联 ...