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 ...
随机推荐
- Nuxt开发经验分享
Nuxt开发经验分享 本文章基于starter-template模板进行讲解,面向有vue-cli开发经验的宝宝 vue init nuxt-community/starter-template ...
- js jquery 判断匹配元素是否存在
jQuery 判断页面元素是否存在的代码 在传统的Javascript里,当我们对某个页面元素进行某种操作前,最好先判断这个元素是否存在.原因是对一个不存在的元素进行操作是不允许的. 例如: 复制代码 ...
- java类的方法
我前面说的都是类的属性,现在说类的方法: 类{ 属性:“类有什么” 方法:“类能做什么” } 首先我们在之前创建的学生类中添加个打印1-10数字的方法,参数是name package cuteSnow ...
- 【 【henuacm2016级暑期训练】动态规划专题 P】Animals
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 第i只动物如果饲养它的话. 代价是固定的就是(n-i+1)a[i] 所以相当于给你n个物品,每个物品的重量为(n-i+1)a[i], ...
- 【codeforces 738E】Subordinates
[题目链接]:http://codeforces.com/problemset/problem/738/E [题意] 给你一个类似树形的关系; 然后告诉你某个人头顶上有多少个上司numi; 只有fat ...
- UVALive 3231 Fair Share
Fair Share Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origina ...
- USACO 5.1.1凸包
转自:http://blog.csdn.net/cnyali/article/details/50097593 程序: #include <iostream> #include <a ...
- 第一个python作业题目以及代码
1. 编写程序,用户输入一个三位以上的整数,输出其百位以上的数字.例如用户输入1234,则程序输出12.(提示:使用整除运算.) x=input("请输入一个三位以上的数字:") ...
- javascript 简单语法 对象属性及方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CCControlExtension/CCControlPotentiometer
#ifndef __CCCONTROLPOTENTIOMETER_H__ #define __CCCONTROLPOTENTIOMETER_H__ #include "CCControl.h ...