Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hrt

Package Overview
Dependencies
Maintainers
3
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hrt

HTTP Rewrite Tool

  • 1.0.25
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
77
increased by120%
Maintainers
3
Weekly downloads
 
Created
Source

HTTP Rewrite Tool

HRT是前端代理工具,根据配置把指定的URL指向到本地文件或远程URL。

安装

npm install hrt -g

使用方法

  1. 修改浏览器代理设置,IP:127.0.0.1,端口:2222,推荐用SwitchySharp(Chrome插件)、FoxyProxy(Firefox插件)切换代理。

  2. 创建配置文件 my-hrt-config.js ,添加跳转规则。

    代理文件:

    exports.map = [
    	['http://js.tudouui.com/v3/dist/js/g.js', 'D:\\project\\static\\trunk\\v3\\src\\js\\g.js']
    ];
    

    代理目录:

    exports.map = [
    	['http://js.tudouui.com/v3/dist/js', 'D:\\project\\static\\trunk\\v3\\src\\js']
    ];
    
  3. 在命令行输入 hrt my-hrt-config.js ,启动HTTP服务。

    # 修改端口
    hrt my-hrt-config.js --port=8080
    # 输出调试信息
    hrt my-hrt-config.js --debug=true
    

高级用法

  1. 移除版本号。

    exports.before = function(url) {
    	return url.replace(/([^?]+)_\d+(\.(?:js|css))/, '$1$2');
    };
    
  2. 修改文件内容。

    exports.merge = function(path, callback) {
    	// 所有JS头部添加注释
    	if (/\.js$/.test(path)) {
    		var content = Util.readFileSync(path, 'utf-8');
    		return callback('application/javascript', '/* test /*\n' + content);
    	}
    	// 其它请求
    	var contentType = require('mime').lookup(path);
    	var buffer = this.util.readFileSync(path);
    	return callback(contentType, buffer);
    };
    

    注:当配置文件里有 exports.merge 时会接管所有请求,所以在程序逻辑里需要加入文件类型判断。

  3. 修改URL内容。

    exports.merge = function(path, callback) {
    	if (/^http:\/\/(www|wwwtest|beta)\.tudou\.com\/programs\/view\//.test(this.req.url)) {
    		this.util.get(this.req.url, 'gbk', function(body) {
    			callback('text/html', body + '<!-- test -->', 'gbk');
    		});
    		return;
    	}
    	// 其它请求
    	var contentType = require('mime').lookup(path);
    	var buffer = this.util.readFileSync(path);
    	return callback(contentType, buffer);
    };
    

Keywords

FAQs

Package last updated on 06 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc