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 ...
随机推荐
- [using_microsoft_infopath_2010]Chapter6 发布提交表单数据
本章概要: 1.使用正确的方法发布表单 2.发布表单画库到SharePoint 3.在发布和保存表单之间做出选择 4.理解不同发布表单方式之间的区别
- Linux下永久改动MAC地址和ifconfig命令总结
1. 固定一个MAC地址,特别是在使用多个虚拟机的时候 linux环境下: 用root身份登录,在/etc/rc.d/rc.local里加上这三句 ifconfig eth0 down ifconfi ...
- sap abap 对字符串的操作
替换字段内容 REPLACE [FIRST /ALL OCCURRENCES OF]<STR1>INTO <STR> WITH <STR2> DATA STR ...
- 剑指offer面试题26-复杂链表的复制
题目: 请实现函数ComplexListNode* Clone(ComplexListNode* pHead).复制一个复杂链表. 在复杂链表中.每个节点除了一个m_pNext指针指向下一个节点外,另 ...
- Medium上的文章
Welcome to Medium, a place to read, write, and interact with the stories that matter most to you. 网站 ...
- MongoDB数据查询详解
查询全部 db.infos.find(); db.infos.find({"url":"www.baidu.com"}); id不要显示出来 db.info ...
- ORA-01261: Parameter db_recovery_file_dest destination string cannot be translat
查看了Oracle的初始化文件. $cat /oracle/oms/102_64/dbs/initSID.ora 如:db_recovery_file_dest='/oracle/oms/flash_ ...
- DateForamt和SimpleDateFormat
1.因为DateFormat是抽象类,所以只能用子类来初始化 DateFormat df = new SimpleDateFormat("yyyy--MM--dd HH:MM:ss,属于本年 ...
- 新版Eclipse找不到Java EE Module Dependencies选项
在 Eclipse Galileo (3.5) 版本或Ganymede (3.4) 等更老的版本中, 你可以使用Java EE Module Dependencies 选项来组织你的项目结构,确保依赖 ...
- 搭建python3环境
将Python2.7通过控制面板卸载,然后从网站下载python的安装包,安装即可. 安装beatifulsoup4库.在命令行下, pip install beautifulsoup4 千万不能在p ...