Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

responderListFilePath as an object #40

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,33 @@ If you don't know how to set proxy for browser, please read this wiki: [How to s
{
pattern: /ui\/(.*)\/img\//,
responder: '/home/$1/image/'
},

// 7. Cache http response into local files
// For Example,
// http://host/someURL.html => ./cache/someURL.html
{
pattern : /host\/(someURL)/,
responder : __dirname + '/cache/$1.html',
cache : true
// use this if you want to use cache for POST requests also
// cachePost : true
},


// 8. delay (fix) a response (to simulate network or so)
{
pattern: 'homepage.js', // Match url you wanna replace
responder: '/home/goddyzhao/workspace/homepage.js',
delay: 2000 // ms
},
// or
{
pattern: 'homepage.js', // Match url you wanna replace
responder: '/home/goddyzhao/workspace/homepage.js',
delay: {min: 200, max: 2000} // random delay between 200 and 2000 ms
}

];

You can use the [template file](https://github.com/goddyzhao/nproxy/blob/master/replace-rule.sample.js) and replace it with your own configurations.
Expand Down
2 changes: 0 additions & 2 deletions TODO.md

This file was deleted.

7 changes: 7 additions & 0 deletions examples/.proxy-auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"protocol" : "http",
"host" : "10.xx.xx.xx",
"port" : 8090,
"username" : "johndoe",
"password" : "/!\\ PUT THIS FILE IN GITIGNORE"
}
38 changes: 38 additions & 0 deletions examples/Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Example of nproxy configuration for grunt
// http://gruntjs.com/

module.exports = function (grunt) {
'use strict';
var nproxy = require('nproxy'),
argv = require('yargs').argv;


grunt.registerTask('nproxy', 'start nproxy to proxy local JS and CSS files', function() {

// Grunt should not finish the task
this.async();

// This will serve static local files for JS & CSS intead of remote ones
// use: `localhost:8989` as proxy parameter

// see `nproxy-conf.js` for proxy configuration
var port = 8989,
options = {
timeout : 100,
debug : !!argv.debug
};

// OPTIONAL: If you want nproxy requests to be proxyfied
try {
var proxyConf = require('./.proxy-auth.json');
options.proxy = proxyConf;
} catch(e) { }

options.responderListFilePath = require('./nproxy-conf.js')(argv);

nproxy(port, options);
});

};


33 changes: 33 additions & 0 deletions examples/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Example of nproxy configuration for gulp
// http://gulpjs.com/

'use strict';

var gulp = require('gulp'),
nproxy = require('nproxy'),
argv = require('yargs').argv;

// This will serve static local files intead of remote ones
// use: `localhost:8989` as proxy parameter

// see `nproxy-conf.js` for proxy configuration

gulp.task('nproxy', function nproxyTask(host) {

var port = 8989,
options = {
timeout : 100,
debug : !!argv.debug
};

// OPTIONAL: If you want nproxy requests to be proxyfied
try {
var proxyConf = require('./.proxy-auth.json');
options.proxy = proxyConf;
} catch(e) { }

options.responderListFilePath = require('./nproxy-conf.js')(argv);

return nproxy(port, options);

});
94 changes: 94 additions & 0 deletions examples/nproxy-conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict';
/* jshint node:true */

// This is the configuration file to bind HTTP requests
// to local files, when using `nproxy` with gulp or grunt

// available CLI arguments:
// `--css` serve only CSS files
// `--js` serve only JS files
// `--no-external` serve external files (some-analytics-provider, some-other-service-provider) as empty

module.exports = function(options) {
var mapping = [],

base = __dirname + '/assets',
css_folder = base + 'css/',
js_folder = base + 'js/',

// `concat.js` will be served as the concatenation of the following files
concat = [
js_folder + 'sample-file-1.js',
js_folder + 'sample-file-2.js',
js_folder + 'sample-file-3.js',
// this file is not included in prod, but it contains dev tools
// (it can be a screen logger for mobile and tablets, etc.)
js_folder + 'dev-debbuging-tools.js'
];


// serve local JS files
// do not include JS if `--css` specified
if(!options.css) {
console.log('SERVING LOCAL JS FILES');

// serve `concat.js`
mapping.push({
pattern : /my-website.*concat\.js/,
responder : concat
});

// other JS files
// http://my-website.com/assets/js/subdir/myfile.js
// $1 = subdir/myfile.js
mapping.push({
pattern : /my-website.*\/js\/(.*\.js)/,
responder : js_folder + '$1'
});

}


// serve local CSS files
// do not include CSS if `--js` specified
if(!options.js) {
console.log('SERVING LOCAL CSS Files');

// CSS files
// http://my-website.com/assets/css/subdir/myfile.css
// $1 = news/article/article.css
mapping.push({
pattern : /my-website.*\/css\/(.*\.css)/,
responder : css_folder + '$1'
});
}

// This will remove external scripts (served as empty files)
// specify `--no-external` to activate this feature
if(options.external === false) {
console.log('NO EXTERNAL (scripts are served empty)');
mapping.push({
pattern : /(some-analytics-provider|some-other-service-provider).*\.js/,
responder : js_folder + 'empty.js'
});
}

// Experimental : cache
// specify `cache: true` to save web content into a local file
// you might want to use a cache folder and add it to `.gitignore`
mapping.push({
pattern : /(someURL\.html)/,
responder : __dirname + '/cache/$1',
cache : true
});

// any other request that doesn't match the rules above will be forwarded

return mapping;

};





9 changes: 9 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "nproxy-grunt-example",
"devDependencies": {
"nproxy": "stephane-tessier/nproxy",
"grunt": "^0.4.5",
"gulp": "^3.9.0",
"yargs": "^3.15.0"
}
}
79 changes: 79 additions & 0 deletions examples/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
__PROJECT DOCUMENTATION EXAMPLE (FRENCH)__

Proxyfier les fichiers JS et CSS
================================================================================

Cette manipulation peut s'avérer très utile pour corriger un bug qui n'est pas reproductible sur la maquette, à cause de données différentes, de scripts tiers, ou tout simplement pour ne pas avoir à faire tourner une lourde VM dont on n'a pas forcément besoin en tant que developpeur front.

Proxyfier ?!
--------------------------------------------------------------------------------

Proxifier signifique utiliser un element tier qui vient se placer entre le serveur et le navigateur. Dans notre cas, le proxy va permettre de naviguer sur un site distant (recette, pre-prod, prod, etc.), mais en chargeant les fichiers JS et/ou CSS locaux (à la place des fichiers distants).


SERVEUR <---------------\
PROXY <-------> NAVIGATEUR
JS ET CSS LOCAUX <------/


Installation
--------------------------------------------------------------------------------

Le proxy est installé en même temps que les autres paquets NodeJS (via la commande ```npm install```).

Démarrer le proxy
--------------------------------------------------------------------------------

Pour démarrer le proxy, se placer dans le dossier racine du projet (contenant le fichier _Gruntfile.js_ ou _gulpfile.js_), et entrez la commande suivante :

```shell
> grunt/gulp nproxy
```

Le proxy '''localhost:8989''' est alors accessible.

Proxyfier le Javascript uniquement :

```shell
> grunt/gulp nproxy --js
```

Proxyfier le CSS uniquement :

```shell
> grunt/gulp nproxy --css
```

Désactiver les scripts externes (e.g. Google Tag Management, Facebook) :

(repectivement ''--no-gmt'', ''--no-facebook'')

```shell
> grunt/gulp --no-externals
```

Pour désactiver ces modules, on dessert un fichier javascript vide au lieu du fichier original.



Utiliser le proxy
--------------------------------------------------------------------------------

Vous pouvez configurer le proxy au niveau du système ou du navigateur avec le paramètre suivant : ```localhost:8989```.

Mais vous l'aurez compris, tout votre trafique va alors passer par votre proxy, ce qui n'est pas vraiment souhaitable (sauf dans le cas d'une VM dédiée, avec par exemple IE).

### Exemple de plugin

De nombreux plugins (extensions de navigateurs) permettent de facilement activer/désactiver le proxy au niveau du navigateur, ainsi que de paramétrer quelles URLs sont proxifiées, lesquelles ne le sont pas.

Voici deux exemples :

* [SwitchyOmega](https://chrome.google.com/webstore/detail/padekgcemlokbadohgkifijomclgjgif) sur Chrome
* [FoxyProxy](https://addons.mozilla.org/fr/firefox/addon/foxyproxy-standard/) sur Firefox


Configuration
--------------------------------------------------------------------------------

La configuration du proxy s'effectue dans le fichier _nproxy-conf.js_
Loading