http-proxy-middleware 0.17.0 → 0.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +21 -6
  3. package/lib/handlers.js +13 -1
  4. package/lib/index.js +33 -10
  5. package/lib/logger.js +2 -2
  6. package/package.json +16 -12
  7. package/.editorconfig +0 -18
  8. package/.jscsrc +0 -8
  9. package/.npmignore +0 -28
  10. package/.travis.yml +0 -16
  11. package/CONTRIBUTING.md +0 -61
  12. package/ISSUE_TEMPLATE.md +0 -21
  13. package/examples/README.md +0 -33
  14. package/examples/browser-sync/index.js +0 -29
  15. package/examples/connect/index.js +0 -29
  16. package/examples/express/index.js +0 -28
  17. package/examples/websocket/index.html +0 -96
  18. package/examples/websocket/index.js +0 -41
  19. package/recipes/README.md +0 -110
  20. package/recipes/basic.md +0 -32
  21. package/recipes/context-matching.md +0 -99
  22. package/recipes/corporate-proxy.md +0 -21
  23. package/recipes/delay.md +0 -37
  24. package/recipes/logLevel.md +0 -40
  25. package/recipes/logProvider.md +0 -77
  26. package/recipes/modify-post.md +0 -74
  27. package/recipes/pathRewrite.md +0 -93
  28. package/recipes/proxy-events.md +0 -74
  29. package/recipes/router.md +0 -83
  30. package/recipes/servers.md +0 -252
  31. package/recipes/shorthand.md +0 -63
  32. package/recipes/virtual-hosts.md +0 -18
  33. package/recipes/websocket.md +0 -41
  34. package/test/e2e/_utils.js +0 -21
  35. package/test/e2e/http-proxy-middleware.spec.js +0 -672
  36. package/test/e2e/path-rewriter.spec.js +0 -98
  37. package/test/e2e/router.spec.js +0 -128
  38. package/test/e2e/websocket.spec.js +0 -150
  39. package/test/unit/_libs.js +0 -8
  40. package/test/unit/config-factory.spec.js +0 -154
  41. package/test/unit/context-matcher.spec.js +0 -250
  42. package/test/unit/handlers.spec.js +0 -126
  43. package/test/unit/logger.spec.js +0 -259
  44. package/test/unit/path-rewriter.spec.js +0 -143
  45. package/test/unit/router.spec.js +0 -136
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## [v0.17.3](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.17.3)
4
+ - fix(onError): improve default proxy error handling. http status codes (504, 502 and 500). ([#132](https://github.com/chimurai/http-proxy-middleware/pull/132)) ([graingert](https://github.com/graingert))
5
+
6
+ ## [v0.17.2](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.17.2)
7
+ - feat(logging): improve error message & add link to Node errors page. ([#106](https://github.com/chimurai/http-proxy-middleware/pull/106)) ([cloudmu](https://github.com/cloudmu))
8
+ - feat(pathRewrite): path can be empty string. ([#110](https://github.com/chimurai/http-proxy-middleware/pull/110)) ([sunnylqm](https://github.com/sunnylqm))
9
+ - bug(websocket): memory leak when option 'ws:true' is used. ([#114](https://github.com/chimurai/http-proxy-middleware/pull/114)) ([julbra](https://github.com/julbra))
10
+ - chore(package.json): reduce package size. ([#109](https://github.com/chimurai/http-proxy-middleware/pull/109))
11
+
12
+ ## [v0.17.1](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.17.1)
13
+ - fix(Express sub Router): 404 on non-proxy routes ([#94](https://github.com/chimurai/http-proxy-middleware/issues/94))
14
+
3
15
  ## [v0.17.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.17.0)
4
16
  - fix(context matching): Use [RFC 3986 path](https://tools.ietf.org/html/rfc3986#section-3.3) in context matching. (excludes query parameters)
5
17
 
package/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  [![Build Status](https://img.shields.io/travis/chimurai/http-proxy-middleware/master.svg?style=flat-square)](https://travis-ci.org/chimurai/http-proxy-middleware)
4
4
  [![Coveralls](https://img.shields.io/coveralls/chimurai/http-proxy-middleware.svg?style=flat-square)](https://coveralls.io/r/chimurai/http-proxy-middleware)
5
5
  [![dependency Status](https://img.shields.io/david/chimurai/http-proxy-middleware.svg?style=flat-square)](https://david-dm.org/chimurai/http-proxy-middleware#info=dependencies)
6
+ [![dependency Status](https://snyk.io/test/npm/http-proxy-middleware/badge.svg)](https://snyk.io/test/npm/http-proxy-middleware)
6
7
 
7
8
  Node.js proxying made simple. Configure proxy middleware with ease for [connect](https://github.com/senchalabs/connect), [express](https://github.com/strongloop/express), [browser-sync](https://github.com/BrowserSync/browser-sync) and [many more](#compatible-servers).
8
9
 
@@ -106,10 +107,10 @@ var options = {
106
107
  changeOrigin: true, // needed for virtual hosted sites
107
108
  ws: true, // proxy websockets
108
109
  pathRewrite: {
109
- '^/old/api' : '/new/api', // rewrite path
110
- '^/remove/api' : '/api' // remove path
110
+ '^/api/old-path' : '/api/new-path', // rewrite path
111
+ '^/api/remove/path' : '/path' // remove base path
111
112
  },
112
- proxyTable: {
113
+ router: {
113
114
  // when request.headers.host == 'dev.localhost:3000',
114
115
  // override target 'http://www.example.org' to 'http://localhost:8000'
115
116
  'dev.localhost:3000' : 'http://localhost:8000'
@@ -303,15 +304,29 @@ The following options are provided by the underlying [http-proxy](https://github
303
304
  * **option.xfwd**: true/false, adds x-forward headers
304
305
  * **option.secure**: true/false, if you want to verify the SSL Certs
305
306
  * **option.toProxy**: true/false, passes the absolute URL as the `path` (useful for proxying to proxies)
306
- * **option.prependPath**: true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
307
- * **option.ignorePath**: true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request>
307
+ * **option.prependPath**: true/false, Default: true - specify whether you want to prepend the target's path to the proxy path
308
+ * **option.ignorePath**: true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request (note: you will have to append / manually if required).
308
309
  * **option.localAddress** : Local interface string to bind for outgoing connections
309
- * **option.changeOrigin**: true/false, adds host to request header.
310
+ * **option.changeOrigin**: true/false, Default: false - changes the origin of the host header to the target URL
310
311
  * **option.auth** : Basic authentication i.e. 'user:password' to compute an Authorization header.
311
312
  * **option.hostRewrite**: rewrites the location hostname on (301/302/307/308) redirects.
312
313
  * **option.autoRewrite**: rewrites the location host/port on (301/302/307/308) redirects based on requested host/port. Default: false.
313
314
  * **option.protocolRewrite**: rewrites the location protocol on (301/302/307/308) redirects to 'http' or 'https'. Default: null.
315
+ * **option.cookieDomainRewrite**: rewrites domain of `set-cookie` headers. Possible values:
316
+ * `false` (default): disable cookie rewriting
317
+ * String: new domain, for example `cookieDomainRewrite: "new.domain"`. To remove the domain, use `cookieDomainRewrite: ""`.
318
+ * Object: mapping of domains to new domains, use `"*"` to match all domains.
319
+ For example keep one domain unchanged, rewrite one domain and remove other domains:
320
+ ```
321
+ cookieDomainRewrite: {
322
+ "unchanged.domain": "unchanged.domain",
323
+ "old.domain": "new.domain",
324
+ "*": ""
325
+ }
326
+ ```
314
327
  * **option.headers**: object, adds [request headers](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields). (Example: `{host:'www.example.org'}`)
328
+ * **option.proxyTimeout**: timeout (in millis) when proxy receives no response from target
329
+
315
330
 
316
331
 
317
332
  ## Shorthand
package/lib/handlers.js CHANGED
@@ -48,9 +48,21 @@ function getProxyEventHandlers(opts) {
48
48
 
49
49
  function defaultErrorHandler(err, req, res) {
50
50
  var host = (req.headers && req.headers.host);
51
+ var code = err.code;
51
52
 
52
53
  if (res.writeHead && !res.headersSent) {
53
- res.writeHead(500);
54
+ if (/HPE_INVALID/.test(code)) {
55
+ res.writeHead(502);
56
+ } else {
57
+ switch(code) {
58
+ case 'ECONNRESET':
59
+ case 'ENOTFOUND':
60
+ case 'ECONNREFUSED':
61
+ res.writeHead(504);
62
+ break;
63
+ default: res.writeHead(500);
64
+ }
65
+ }
54
66
  }
55
67
 
56
68
  res.end('Error occured while trying to proxy to: ' + host + req.url);
package/lib/index.js CHANGED
@@ -13,6 +13,7 @@ module.exports = HttpProxyMiddleware;
13
13
  function HttpProxyMiddleware(context, opts) {
14
14
  // https://github.com/chimurai/http-proxy-middleware/issues/57
15
15
  var wsUpgradeDebounced = _.debounce(handleUpgrade);
16
+ var wsInitialized = false;
16
17
  var config = configFactory.createConfig(context, opts);
17
18
  var proxyOptions = config.options;
18
19
 
@@ -35,10 +36,7 @@ function HttpProxyMiddleware(context, opts) {
35
36
  return middleware;
36
37
 
37
38
  function middleware(req, res, next) {
38
- // https://github.com/chimurai/http-proxy-middleware/issues/17
39
- req.url = req.originalUrl;
40
-
41
- if (contextMatcher.match(config.context, req.url, req)) {
39
+ if (shouldProxy(config.context, req)) {
42
40
  var activeProxyOptions = prepareProxyRequest(req);
43
41
  proxy.web(req, res, activeProxyOptions);
44
42
  } else {
@@ -46,22 +44,42 @@ function HttpProxyMiddleware(context, opts) {
46
44
  }
47
45
 
48
46
  if (proxyOptions.ws === true) {
47
+ // use initial request to access the server object to subscribe to http upgrade event
49
48
  catchUpgradeRequest(req.connection.server);
50
49
  }
51
50
  }
52
51
 
53
52
  function catchUpgradeRequest(server) {
54
- server.on('upgrade', wsUpgradeDebounced);
53
+ // subscribe once; don't subscribe on every request...
54
+ // https://github.com/chimurai/http-proxy-middleware/issues/113
55
+ if (!wsInitialized) {
56
+ server.on('upgrade', wsUpgradeDebounced);
57
+ wsInitialized = true;
58
+ }
55
59
  }
56
60
 
57
61
  function handleUpgrade(req, socket, head) {
58
- if (contextMatcher.match(config.context, req.url, req)) {
62
+ // set to initialized when used externally
63
+ wsInitialized = true;
64
+
65
+ if (shouldProxy(config.context, req)) {
59
66
  var activeProxyOptions = prepareProxyRequest(req);
60
67
  proxy.ws(req, socket, head, activeProxyOptions);
61
68
  logger.info('[HPM] Upgrading to WebSocket');
62
69
  }
63
70
  }
64
71
 
72
+ /**
73
+ * Determine whether request should be proxied.
74
+ *
75
+ * @private
76
+ * @return {Boolean}
77
+ */
78
+ function shouldProxy(context, req) {
79
+ var path = (req.originalUrl || req.url);
80
+ return contextMatcher.match(context, path, req);
81
+ }
82
+
65
83
  /**
66
84
  * Apply option.router and option.pathRewrite
67
85
  * Order matters:
@@ -69,6 +87,10 @@ function HttpProxyMiddleware(context, opts) {
69
87
  NOT the modified path, after it has been rewritten by pathRewrite
70
88
  */
71
89
  function prepareProxyRequest(req) {
90
+ // https://github.com/chimurai/http-proxy-middleware/issues/17
91
+ // https://github.com/chimurai/http-proxy-middleware/issues/94
92
+ req.url = (req.originalUrl || req.url);
93
+
72
94
  // store uri before it gets rewritten for logging
73
95
  var originalPath = req.url;
74
96
  var newProxyOptions = _.cloneDeep(proxyOptions);
@@ -107,7 +129,7 @@ function HttpProxyMiddleware(context, opts) {
107
129
  if (pathRewriter) {
108
130
  var path = pathRewriter(req.url, req);
109
131
 
110
- if (path) {
132
+ if (typeof path === 'string') {
111
133
  req.url = path;
112
134
  } else {
113
135
  logger.info('[HPM] pathRewrite: No rewritten path found. (%s)', req.url);
@@ -116,9 +138,10 @@ function HttpProxyMiddleware(context, opts) {
116
138
  }
117
139
 
118
140
  function logError(err, req, res) {
119
- var hostname = (req.hostname || req.host) || (req.headers && req.headers.host); // (node0.10 || node 4/5) || (websocket)
120
- var targetUri = (proxyOptions.target.host || proxyOptions.target) + req.url;
141
+ var hostname = (req.headers && req.headers.host) || (req.hostname || req.host); // (websocket) || (node0.10 || node 4/5)
142
+ var target = proxyOptions.target.host || proxyOptions.target;
143
+ var errReference = 'https://nodejs.org/api/errors.html#errors_common_system_errors'; // link to Node Common Systems Errors page
121
144
 
122
- logger.error('[HPM] PROXY ERROR: %s. %s -> %s', err.code, hostname, targetUri);
145
+ logger.error('[HPM] Error occurred while trying to proxy request %s from %s to %s (%s) (%s)', req.url, hostname, target, err.code, errReference);
123
146
  }
124
147
  };
package/lib/logger.js CHANGED
@@ -143,9 +143,9 @@ function Logger() {
143
143
 
144
144
  /**
145
145
  * -> normal proxy
146
- * => proxyTable
146
+ * => router
147
147
  * ~> pathRewrite
148
- * ≈> proxyTable + pathRewrite
148
+ * ≈> router + pathRewrite
149
149
  */
150
150
  function getArrow(originalPath, newPath, originalTarget, newTarget) {
151
151
  var arrow = ['>'];
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "http-proxy-middleware",
3
- "version": "0.17.0",
3
+ "version": "0.17.3",
4
4
  "description": "The one-liner node.js proxy middleware for connect, express and browser-sync",
5
5
  "main": "index.js",
6
+ "files": [
7
+ "index.js",
8
+ "lib"
9
+ ],
6
10
  "scripts": {
7
11
  "clean": "rm -rf coverage",
8
12
  "test": "mocha --recursive --colors --reporter spec",
@@ -35,22 +39,22 @@
35
39
  },
36
40
  "homepage": "https://github.com/chimurai/http-proxy-middleware",
37
41
  "devDependencies": {
38
- "browser-sync": "^2.13.0",
42
+ "browser-sync": "^2.18.2",
39
43
  "chai": "^3.5.0",
40
- "connect": "^3.4.1",
41
- "coveralls": "^2.11.9",
42
- "express": "^4.13.4",
43
- "istanbul": "^0.4.3",
44
+ "connect": "^3.5.0",
45
+ "coveralls": "^2.11.15",
46
+ "express": "^4.14.0",
47
+ "istanbul": "^0.4.5",
44
48
  "istanbul-coveralls": "^1.0.3",
45
- "mocha": "^2.5.3",
49
+ "mocha": "^3.2.0",
46
50
  "mocha-lcov-reporter": "1.2.0",
47
51
  "opn": "^4.0.2",
48
- "ws": "^1.1.0"
52
+ "ws": "^1.1.1"
49
53
  },
50
54
  "dependencies": {
51
- "http-proxy": "^1.13.3",
52
- "is-glob": "^2.0.1",
53
- "lodash": "^4.13.1",
54
- "micromatch": "^2.3.8"
55
+ "http-proxy": "^1.16.2",
56
+ "is-glob": "^3.1.0",
57
+ "lodash": "^4.17.2",
58
+ "micromatch": "^2.3.11"
55
59
  }
56
60
  }
package/.editorconfig DELETED
@@ -1,18 +0,0 @@
1
- # http://editorconfig.org
2
-
3
- root = true
4
-
5
- [*]
6
- charset = utf-8
7
- end_of_line = lf
8
- indent_style = space
9
- indent_size = 4
10
- insert_final_newline = true
11
- trim_trailing_whitespace = true
12
-
13
- [*.md]
14
- trim_trailing_whitespace = false
15
-
16
- # Matches the exact files either package.json or .travis.yml
17
- [{package.json,.travis.yml}]
18
- indent_size = 2
package/.jscsrc DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "preset": "google",
3
- "validateIndentation": 4,
4
- "maximumLineLength": {
5
- "value": 120,
6
- "allExcept": ["comments", "regex"]
7
- },
8
- }
package/.npmignore DELETED
@@ -1,28 +0,0 @@
1
- # Logs
2
- logs
3
- *.log
4
-
5
- # Runtime data
6
- pids
7
- *.pid
8
- *.seed
9
-
10
- # Directory for instrumented libs generated by jscoverage/JSCover
11
- lib-cov
12
-
13
- # Coverage directory used by tools like istanbul
14
- coverage
15
-
16
- # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17
- .grunt
18
-
19
- # Compiled binary addons (http://nodejs.org/api/addons.html)
20
- build/Release
21
-
22
- # Dependency directory
23
- # Commenting this out is preferred by some people, see
24
- # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
25
- node_modules
26
-
27
- # Users Environment Variables
28
- .lock-wscript
package/.travis.yml DELETED
@@ -1,16 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - 'stable'
4
- - '5'
5
- - '4'
6
- - '0.12'
7
- - '0.11'
8
- - '0.10'
9
- before_install:
10
- # https://github.com/npm/npm/issues/11283
11
- - npm set progress=false
12
- after_success: npm run coveralls
13
- env:
14
- global:
15
- # COVERALLS_REPO_TOKEN=
16
- secure: "I8mDH0n2DQHAPvUFEV/ZNmrMNYTJxgywg8+P3yuCAWwQkZeXQi0DWG+6VUpOylaRhL/4kCdZK9gnJD2MfrqvNqVLDUqBK3tTmZVoyqqJEdE0UdEHcPncAPmxWrG98sDjqFN9r4nWeizHvyA1fNRQHRN56Zy+DcQWjgEhHJaYeNA="
package/CONTRIBUTING.md DELETED
@@ -1,61 +0,0 @@
1
- # Contributing
2
-
3
- We love contributors! Your help is welcome to make this project better!
4
-
5
- Some simple guidelines we'd like you to follow.
6
-
7
- ## Got Questions or Problems?
8
-
9
- If you have questions about http-proxy-middle usage; Please check if your question hasn't been already answered on [Stack Overflow](http://stackoverflow.com/search?q=%22http-proxy-middleware%22) or in our [issue archive](https://github.com/chimurai/http-proxy-middleware/issues?utf8=%E2%9C%93&q=is%3Aissue+), [examples](https://github.com/chimurai/http-proxy-middleware/tree/master/examples) and [recipes](https://github.com/chimurai/http-proxy-middleware/tree/master/recipes).
10
-
11
- Since Nodejitsu's `http-proxy` is providing the actual proxy functionality; You might find your answer in their [documentation](https://github.com/nodejitsu/node-http-proxy), [issue archive](https://github.com/nodejitsu/node-http-proxy/issues?utf8=%E2%9C%93&q=is%3Aissue) or [examples](https://github.com/nodejitsu/node-http-proxy/tree/master/examples).
12
-
13
- Feel free to [ask a question](https://github.com/chimurai/http-proxy-middleware/issues) if the above resources didn't answer your question.
14
-
15
- Tips on how to ask: http://stackoverflow.com/help/how-to-ask
16
-
17
- ## Report Issues
18
-
19
- If you think you've found an issue, please submit it to the [Github issue tracker](https://github.com/chimurai/http-proxy-middleware/issues).
20
-
21
- "*[It doesn't work](https://goo.gl/GzkkTg)*" is not very useful for anyone.
22
- A good issue report should have a well described **problem description** and proxy **configuration**. A great issue report includes a **minimal example**.
23
-
24
- Properly format your code example for easier reading: [Code and Syntax Highlighting](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code-and-syntax-highlighting).
25
-
26
- The quality of your issue report will determine how quickly and deeply we'll delve into it. Some simple pointers for reporting an issue:
27
-
28
- #### Problem Description
29
-
30
- - describe the problem
31
- - steps taken (request urls)
32
- - found
33
- - expected
34
-
35
- #### Configuration / Setup
36
-
37
- - http-proxy-middleware version
38
- - http-proxy-middleware configuration
39
-
40
- It might be useful to provide server information in which http-proxy-middleware is used and the target server information to which requests are being proxied.
41
-
42
- - server + version (express, connect, browser-sync, etc...)
43
- + server port number
44
- - target server
45
- + target server port number
46
-
47
- ### Minimal example
48
-
49
- Provide a minimal example to exclude external factors. This will greatly help identifying the issue.
50
-
51
- Tips on how to create a minimal example: http://stackoverflow.com/help/mcve
52
-
53
- ## New Feature?
54
-
55
- Request a new feature by submitting an issue into our [Github issue tracker](https://github.com/chimurai/http-proxy-middleware/issues).
56
-
57
- PRs are welcome. Please discuss it in our [Github issue tracker](https://github.com/chimurai/http-proxy-middleware/issues) before you start working on it, to avoid wasting your time and effort in case we are already (planning to) working on it.
58
-
59
- ## Documentation
60
-
61
- Feel free to send PRs to improve the documentation, [examples](https://github.com/chimurai/http-proxy-middleware/tree/master/examples) and [recipes](https://github.com/chimurai/http-proxy-middleware/tree/master/recipes).
package/ISSUE_TEMPLATE.md DELETED
@@ -1,21 +0,0 @@
1
- ### Expected behavior
2
-
3
- ### Actual behavior
4
-
5
- ### Setup
6
-
7
- * http-proxy-middleware: _version_
8
- * server: _connect/express/browser-sync..._ + _version_
9
- * other relevant modules
10
-
11
- #### proxy middleware configuration
12
- ```javascript
13
- var apiProxy = proxy('/api', {target:'http://www.example.org', changeOrigin:true});
14
- ```
15
- #### server mounting
16
- ```javascript
17
- var app = express();
18
-
19
- app.use(apiProxy);
20
- app.listen(3000);
21
- ```
@@ -1,33 +0,0 @@
1
- # Examples
2
-
3
- View working examples of `http-proxy-middleware` implemented in popular servers.
4
-
5
- To run and view the [examples](https://github.com/chimurai/http-proxy-middleware/tree/master/examples); Clone the `http-proxy-middleware` repo and install the dependencies:
6
-
7
- ```bash
8
- $ git clone https://github.com/chimurai/http-proxy-middleware.git
9
- $ cd http-proxy-middleware
10
- $ npm install
11
- ```
12
-
13
- Run the example from root folder:
14
-
15
- ```bash
16
- $ node examples/browser-sync
17
- ```
18
-
19
- ```bash
20
- $ node examples/connect
21
- ```
22
-
23
- ```bash
24
- $ node examples/express
25
- ```
26
-
27
- ```bash
28
- $ node examples/websocket
29
- ```
30
-
31
- ## Server recipes
32
-
33
- You can find more server implementations with `http-proxy-middleware` in the [server recipes](https://github.com/chimurai/http-proxy-middleware/tree/master/recipes/servers.md)
@@ -1,29 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
- var browserSync = require('browser-sync').create();
5
- var proxy = require('../../index'); // require('http-proxy-middleware');
6
-
7
- /**
8
- * Configure proxy middleware
9
- */
10
- var jsonPlaceholderProxy = proxy('/users', {
11
- target: 'http://jsonplaceholder.typicode.com',
12
- changeOrigin: true, // for vhosted sites, changes host header to match to target's host
13
- logLevel: 'debug'
14
- });
15
-
16
- /**
17
- * Add the proxy to browser-sync
18
- */
19
- browserSync.init({
20
- server: {
21
- baseDir: './',
22
- port: 3000,
23
- middleware: [jsonPlaceholderProxy],
24
- },
25
- startPath: '/users'
26
- });
27
-
28
- console.log('[DEMO] Server: listening on port 3000');
29
- console.log('[DEMO] Opening: http://localhost:3000/users');
@@ -1,29 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
- var http = require('http');
5
- var connect = require('connect');
6
- var proxy = require('../../index'); // require('http-proxy-middleware');
7
-
8
- /**
9
- * Configure proxy middleware
10
- */
11
- var jsonPlaceholderProxy = proxy({
12
- target: 'http://jsonplaceholder.typicode.com',
13
- changeOrigin: true, // for vhosted sites, changes host header to match to target's host
14
- logLevel: 'debug'
15
- });
16
-
17
- var app = connect();
18
-
19
- /**
20
- * Add the proxy to connect
21
- */
22
- app.use('/users', jsonPlaceholderProxy);
23
-
24
- http.createServer(app).listen(3000);
25
-
26
- console.log('[DEMO] Server: listening on port 3000');
27
- console.log('[DEMO] Opening: http://localhost:3000/users');
28
-
29
- require('opn')('http://localhost:3000/users');
@@ -1,28 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
- var express = require('express');
5
- var proxy = require('../../index'); // require('http-proxy-middleware');
6
-
7
- /**
8
- * Configure proxy middleware
9
- */
10
- var jsonPlaceholderProxy = proxy({
11
- target: 'http://jsonplaceholder.typicode.com',
12
- changeOrigin: true, // for vhosted sites, changes host header to match to target's host
13
- logLevel: 'debug'
14
- });
15
-
16
- var app = express();
17
-
18
- /**
19
- * Add the proxy to express
20
- */
21
- app.use('/users', jsonPlaceholderProxy);
22
-
23
- app.listen(3000);
24
-
25
- console.log('[DEMO] Server: listening on port 3000');
26
- console.log('[DEMO] Opening: http://localhost:3000/users');
27
-
28
- require('opn')('http://localhost:3000/users');
@@ -1,96 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <title>http-proxy-middleware - WebSocket example</title>
6
- <style>
7
- fieldset {
8
- border: 0;
9
- }
10
- label {
11
- display: inline-block;
12
- width: 5em;
13
- vertical-align: top;
14
- }
15
- code {
16
- background-color: #eee;
17
- }
18
- </style>
19
- </head>
20
-
21
- <body>
22
-
23
- <h2>WebSocket demo</h2>
24
-
25
- <p>
26
- Proxy <code>ws://localhost:3000</code> to <code>ws://echo.websocket.org</code>
27
- </p>
28
-
29
- <fieldset id="configuration">
30
- <p>
31
- <label for="location">location:</label>
32
- <input id="location" type="text" value="ws://localhost:3000">
33
- <button id="connect">connect</button>
34
- <button id="disconnect" disabled="disabled">disconnect</button>
35
- </p>
36
- </fieldset>
37
- <fieldset id="messaging" disabled="disabled">
38
- <p>
39
- <label for="message">message:</label>
40
- <input type="text" id="message" value="Hello WebSocket">
41
- <button id="send">send</button>
42
- </p>
43
- <p>
44
- <label for="logger">log:</label>
45
- <textarea id="logger" cols="30" rows="10"></textarea>
46
- </p>
47
- </fieldset>
48
-
49
- <script>
50
- window.onload = function () {
51
- // elements
52
- var configuration = document.getElementById('configuration');
53
- var location = document.getElementById('location');
54
- var connect = document.getElementById('connect');
55
- var disconnect = document.getElementById('disconnect');
56
- var messaging = document.getElementById('messaging');
57
- var message = document.getElementById('message');
58
- var send = document.getElementById('send');
59
- var logger = document.getElementById('logger');
60
-
61
- // ws
62
- var socket;
63
-
64
- connect.onclick = function () {
65
- connect.disabled = true;
66
- disconnect.disabled = false;
67
- messaging.disabled = false;
68
-
69
- socket = new WebSocket(location.value);
70
- socket.onopen = function () { log('CONNECTED'); };
71
- socket.onclose = function () { log('DISCONNECTED'); };
72
- socket.onerror = function () { log('SOCKET ERROR OCCURED'); };
73
- socket.onmessage = function (msg) { log('RECEIVED:' + msg.data); };
74
- }
75
-
76
- disconnect.onclick = function () {
77
- connect.disabled = false;
78
- disconnect.disabled = true;
79
- messaging.disabled = true;
80
- socket.close();
81
- }
82
-
83
- send.onclick = function () {
84
- log('SEND: ' + message.value);
85
- socket.send(message.value);
86
- };
87
-
88
- function log (message) {
89
- logger.value = logger.value + message + '\n'
90
- }
91
-
92
- }
93
- </script>
94
-
95
- </body>
96
- </html>