博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
node anyproxy ssi简易支持
阅读量:4972 次
发布时间:2019-06-12

本文共 4603 字,大约阅读时间需要 15 分钟。

在项目中,ssi include是一个比较常用的功能,这样我们就可以通过web服务器的支持,将公用的html提取出来,改一个文件就会修改全部内容

但是这也带来了问题,在开发的时候没办法的刷新查看,需要提交测试服务器才可以,当然也可以通过在本地通过nginx服务器做到这些。

现在我们有了nodejs,github上面也有很多好的本地代理项目,anyproxy,whistle,livepool...

平时主要用 whistle 操作配置上面都很简单,通过简单配置也可以实现很多功能

livepool,装了下,看着想fiddler,操作起来主要感觉主要是界面操作,就没怎么用

anyproxy,这个是需要自己写js,定义规则的,灵活性好些,但是写代码总归麻烦的

今天通过对比,感觉anyproxy实现node ssi功能比较简单些,其实也很简单,详见代码 时间有限,都是通过同步api写的,略有遗憾

/*  read the following wiki before using rule file  https://github.com/alibaba/anyproxy/wiki/What-is-rule-file-and-how-to-write-one*/"use strict";const fs = require('fs');const url = require("url");const path = require("path");var _getNewFPath = function (newUrl) {    var urlObject = url.parse(newUrl, true);    return path.join(this.hostFilePath, urlObject.pathname);}const config = [    {        urlInclude: 'http://mall.snail.com/pickCard/',        hostFilePath: 'D:/snailshop/mall-snail-com/mall',        ssi: true,        getNewFPath: function (newUrl) {            var urlObject = url.parse(newUrl, true);            if (newUrl.indexOf('http://mall.snail.com/includes/') != -1) {                return path.join('D:/snailshop/mall-snail-com/', urlObject.pathname);            }            return path.join(this.hostFilePath, urlObject.pathname);        }    },    {        urlInclude: 'http://static.snail.com/',        hostFilePath: 'D:/snailshop/mall-snail-com/public',        getNewFPath: _getNewFPath    }];var map = {};var ssiReadFile = function (reqUrl, ssiOpt) {    map = {};    return _ssiReadFile(reqUrl, ssiOpt, [])};var _ssiReadFile = function (reqUrl, ssiOpt, accessPath) {    var readFPath = ssiOpt.getNewFPath(reqUrl);    console.log('_ssiReadFile1', readFPath)    var stat = fs.statSync(readFPath);    var content;    if (stat && stat.isFile()) {        content = fs.readFileSync(readFPath, {encoding: 'utf8'});        var match = content.match(/
/g); console.log('_ssiReadFile2', match); if (match) { match.forEach((m) => { var fileStr = m.match(/
/)[2]; var newUrl = url.resolve(reqUrl, fileStr); if (accessPath.indexOf(newUrl) != -1) {
//存在循环 map[newUrl] = "loop " + newUrl +' in [ '+ accessPath.join() + ']'; } else if (!map[newUrl]) { var _access = accessPath.slice(0); _access.push(newUrl); map[newUrl] = _ssiReadFile(newUrl, ssiOpt, _access); } }) content = content.replace(/
/g, function (a, b, fileStr) { var newUrl = url.resolve(reqUrl, fileStr); return map[newUrl]; }); } } else { content = 'not find file ' + readFPath; } return content;}module.exports = { summary: function(){ return "mall 项目"; }, replaceRequestOption : function(req, option){ var host = option.headers.host; if(host == "mall.snail.com" || host == "static.snail.com" || host == "m.mall.snail.com"){ option.hostname = "10.13.0.206"; option.port = "80"; } else if (host == "mobile.snail.com" || host == "m.mobile.snail.com"){ option.hostname = "10.13.0.205"; option.port = "80"; } else if (host == "cmsv3.snailsite.com"){ option.hostname = "10.110.1.3"; option.port = "80"; } return option; }, shouldInterceptHttpsReq :function(req){ return false; }, shouldUseLocalResponse : function(req, reqBody){ var reqUrl = req.url; var urlObject = url.parse(reqUrl, true); for (var i = 0, len = config.length; i < len ; i++) { let opt = config[i]; if (reqUrl.indexOf(opt.urlInclude) != -1) { let newPath = path.join(opt.hostFilePath, urlObject.pathname); let stat = fs.statSync(newPath); if (stat && stat.isFile()) { console.log('shouldUseLocalResponse', newPath) req.ssi = opt.ssi || false; req.ssiOpt = opt; return true; } } } return false; }, dealLocalResponse : function(req, reqBody, callback){ console.log('dealLocalResponse', req.ssi); if (req.ssi) { callback(200, {"content-type": "text/html"}, ssiReadFile(req.url, req.ssiOpt)); } else { callback(200, {}, fs.readFileSync(req.ssiOpt.getNewFPath(req.url))); } delete req.ssiOpt; delete req.ssi; }};

 

转载于:https://www.cnblogs.com/legu/p/5712317.html

你可能感兴趣的文章
java使用jsp servlet来防止csrf 攻击的实现方法
查看>>
缓存穿透/击穿/雪崩/降级
查看>>
我的作品
查看>>
【bzoj题解】2186 莎拉公主的困惑
查看>>
Protocol Buffer学习笔记
查看>>
Update 语句
查看>>
HBuilder打包Android apk 支付不了问题解决
查看>>
poj2594——最小路径覆盖
查看>>
程序员口述:我是如何工作三年后跳槽到美团的?
查看>>
欧拉函数
查看>>
关于SQL2008 “不允许保存更改。您所做的更改要求删除并重新创建以下表。您对无法重新创建的标进行了更改或者启用了‘阻止保存要求重新创建表的更改’” 解决方案...
查看>>
php文件操作(上传文件)2
查看>>
linux内核驱动模型
查看>>
给WebApp加一个“壳”,实现Andriod系统添加到桌面
查看>>
js 浏览器复制功能
查看>>
数据库总编
查看>>
redis 字符串(string)函数
查看>>
杭州电 1372 Knight Moves(全站搜索模板称号)
查看>>
POJ--3268--Silver Cow Party【SPFA+邻接表】
查看>>
c语言的几个简单memo
查看>>