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

http-proxy-middleware

Package Overview
Dependencies
Maintainers
1
Versions
88
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-proxy-middleware - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

lib/context-matcher.js

2

examples/browser-sync/index.js

@@ -19,3 +19,3 @@ /**

port: 3000,
middleware: [proxy],
middleware: [proxy], // add the proxy to browser-sync
open: false

@@ -22,0 +22,0 @@ }

@@ -17,3 +17,3 @@ /**

var app = connect();
app.use(proxy);
app.use(proxy); // add the proxy to connect

@@ -20,0 +20,0 @@ http.createServer(app).listen(3000);

@@ -16,3 +16,3 @@ /**

var app = express();
app.use(proxy);
app.use(proxy); // add the proxy to express

@@ -19,0 +19,0 @@ app.listen(3000);

@@ -1,4 +0,5 @@

var httpProxy = require('http-proxy');
var utils = require('./lib/utils');
var handlers = require('./lib/handlers');
var httpProxy = require('http-proxy');
var handlers = require('./lib/handlers');
var contextMatcher = require('./lib/context-matcher');
var pathRewriter = require('./lib/path-rewriter');

@@ -14,3 +15,3 @@ var httpProxyMiddleware = function (context, opts) {

console.log('[HPM] Deprecated "option.proxyHost"');
console.log(' Use "option.headers.host" instead');
console.log(' Use "option.changeOrigin" or "option.headers.host" instead');
console.log(' "option.proxyHost" will be removed in future release.');

@@ -28,6 +29,6 @@ console.log('*************************************');

if (proxyOptions.pathRewrite) {
var pathRewriter = utils.createPathRewriter(proxyOptions.pathRewrite);
var rewriter = pathRewriter.create(proxyOptions.pathRewrite);
proxy.on('proxyReq', function (proxyReq, req, res, options) {
handlers.proxyPathRewrite(proxyReq, pathRewriter);
handlers.proxyPathRewrite(proxyReq, rewriter);
});

@@ -41,3 +42,3 @@ }

console.log('[HPM] Proxy created:', context, proxyOptions.target);
console.log('[HPM] Proxy created:', context, ' -> ', proxyOptions.target);

@@ -47,3 +48,3 @@ return middleware;

function middleware (req, res, next) {
if (utils.hasContext(context, req.url)) {
if (contextMatcher.match(context, req.url)) {
proxy.web(req, res);

@@ -50,0 +51,0 @@ } else {

{
"name": "http-proxy-middleware",
"version": "0.1.0",
"description": "The one-liner http-proxy middleware",
"version": "0.2.0",
"description": "The one-liner proxy middleware for connect, express and browser-sync",
"main": "index.js",

@@ -6,0 +6,0 @@ "scripts": {

@@ -6,3 +6,2 @@ # http-proxy-middleware

[![devDependency Status](https://img.shields.io/david/dev/chimurai/http-proxy-middleware.svg?style=flat-square)](https://david-dm.org/chimurai/http-proxy-middleware#info=devDependencies)
[![MIT license](https://img.shields.io/npm/l/http-proxy-middleware.svg?style=flat-square)](https://www.npmjs.com/package/http-proxy-middleware)

@@ -17,9 +16,22 @@ The one-liner proxy middleware for [connect](https://github.com/senchalabs/connect), [express](https://github.com/strongloop/express) and [browser-sync](https://github.com/BrowserSync/browser-sync)

## Core concept
Create and configure the proxy middleware.
Configure the proxy middleware.
```javascript
var proxyMiddleware = require('http-proxy-middleware');
var proxy = proxyMiddleware('/api', {target: 'http://www.example.org'});
// \____/ \________________________________/
// | |
// context options
// 'proxy' is now ready to be used in a server.
```
* **context**: matches provided context against request-urls' path.
Matching requests will be proxied to the target host.
Example: `'/api'` or `['/api', '/ajax']`
* **options.target**: target host to proxy to.
Check out available [proxy options](#options).
## Example

@@ -47,5 +59,2 @@ ```javascript

* `context` path to proxy. Example: '/api'
* `options.target` target host to proxy to. (See "[Options](#options)" for all options)
**Tip:** For [name-based virtual hosted sites](http://en.wikipedia.org/wiki/Virtual_hosting#Name-based), you'll need to use the option `changeOrigin` and set it to `true`.

@@ -63,3 +72,3 @@

* (DEPRECATED) **option.proxyHost**: Use `option.changeOrigin = true` instead.
* **option.pathRewrite**: object, rewrite the url path. Object-keys will be used as _RegEx_ to match paths.
* **option.pathRewrite**: object, rewrite target's url path. Object-keys will be used as _RegExp_ to match paths.

@@ -110,6 +119,6 @@ ```javascript

Or just [explore the examples sources](https://github.com/chimurai/http-proxy-middleware/tree/master/examples):
* `examples/connect` - Example usage with [connect](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect)
* `examples/express` - Example usage with [express](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express)
* `examples/browser-sync` - Example usage with [browser-sync](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync)
Or just explore the [proxy examples](https://github.com/chimurai/http-proxy-middleware/tree/master/examples) sources:
* `examples/connect` - [connect proxy middleware example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect)
* `examples/express` - [express proxy middleware example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express)
* `examples/browser-sync` - [browser-sync proxy middleware example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync)

@@ -116,0 +125,0 @@ ## Tests

@@ -42,20 +42,23 @@ var expect = require('chai').expect;

describe('http-proxy-middleware in actual server', function () {
var servers;
describe('basic setup', function () {
describe('basic setup, requests to target', function () {
var proxyServer, targetServer;
var targetHeaders;
var targetUrl;
var responseBody;
beforeEach(function (done) {
servers = createServers({
proxy: proxyMiddleware('/api', {target:'http://localhost:8000'}),
sourceMiddleware : function (req, res, next) {next()},
targetMiddleware: function (req, res, next) {
targetHeaders = req.headers; // store target headers.
res.write('HELLO WEB'); // respond with 'HELLO WEB'
res.end()
},
});
var mw_proxy = proxyMiddleware('/api', {target:'http://localhost:8000'});
http.get('http://localhost:3000/api/', function (res) {
var mw_target = function (req, res, next) {
targetUrl = req.url; // store target url.
targetHeaders = req.headers; // store target headers.
res.write('HELLO WEB'); // respond with 'HELLO WEB'
res.end()
};
proxyServer = createServer(3000, mw_proxy);
targetServer = createServer(8000, mw_target);
http.get('http://localhost:3000/api/b/c/d;p?q=1&r=[2,3]#s"', function (res) {
res.on('data', function (chunk) {

@@ -68,2 +71,6 @@ responseBody = chunk.toString();

afterEach(function () {
proxyServer.close();
targetServer.close();
});

@@ -74,2 +81,6 @@ it('should have the same headers.host value', function () {

it('should have proxied the uri-path and uri-query, but not the uri-hash', function () {
expect(targetUrl).to.equal('/api/b/c/d;p?q=1&r=[2,3]');
});
it('should have response body: "HELLO WEB"', function () {

@@ -80,19 +91,99 @@ expect(responseBody).to.equal('HELLO WEB');

describe('multi path', function () {
var proxyServer, targetServer;
var targetHeaders;
var response, responseBody;
beforeEach(function () {
var mw_proxy = proxyMiddleware(['/api', '/ajax'], {target:'http://localhost:8000'});
var mw_target = function (req, res, next) {
res.write(req.url); // respond with req.url
res.end()
};
proxyServer = createServer(3000, mw_proxy);
targetServer = createServer(8000, mw_target);
});
afterEach(function () {
proxyServer.close();
targetServer.close();
});
describe('request to path A, configured', function () {
beforeEach(function (done) {
http.get('http://localhost:3000/api/some/endpoint', function (res) {
response = res;
res.on('data', function (chunk) {
responseBody = chunk.toString();
done();
});
});
});
it('should proxy to path A', function () {
expect(response.statusCode).to.equal(200);
expect(responseBody).to.equal('/api/some/endpoint');
});
});
describe('request to path B, configured', function () {
beforeEach(function (done) {
http.get('http://localhost:3000/ajax/some/library', function (res) {
response = res;
res.on('data', function (chunk) {
responseBody = chunk.toString();
done();
});
});
});
it('should proxy to path B', function () {
expect(response.statusCode).to.equal(200);
expect(responseBody).to.equal('/ajax/some/library');
});
});
describe('request to path C, not configured', function () {
beforeEach(function (done) {
http.get('http://localhost:3000/lorum/ipsum', function (res) {
response = res;
res.on('data', function (chunk) {
responseBody = chunk.toString();
done();
});
});
});
it('should not proxy to this path', function () {
expect(response.statusCode).to.equal(404);
});
});
});
describe('additional request headers', function () {
var proxyServer, targetServer;
var targetHeaders;
beforeEach(function (done) {
servers = createServers({
proxy: proxyMiddleware('/api', {target:'http://localhost:8000', headers: {host:'foobar.dev'} }),
sourceMiddleware : function (req, res, next) {next()},
targetMiddleware: function (req, res, next) {
targetHeaders = req.headers; // host
res.end();
},
});
var mw_proxy = proxyMiddleware('/api', { target:'http://localhost:8000', headers: {host:'foobar.dev'} });
var mw_target = function (req, res, next) {
targetHeaders = req.headers;
res.end();
};
proxyServer = createServer(3000, mw_proxy);
targetServer = createServer(8000, mw_target);
http.get('http://localhost:3000/api/', function (res) {
done();
});
});
afterEach(function () {
proxyServer.close();
targetServer.close();
});

@@ -106,20 +197,27 @@

describe('legacy proxyHost parameter', function () {
var proxyServer, targetServer;
var targetHeaders;
beforeEach(function (done) {
servers = createServers({
proxy: proxyMiddleware('/api', {target:'http://localhost:8000', proxyHost: 'foobar.dev'}),
sourceMiddleware : function (req, res, next) {next()},
targetMiddleware: function (req, res, next) {
targetHeaders = req.headers; // host
res.end();
},
});
var mw_proxy = proxyMiddleware('/api', {target:'http://localhost:8000', proxyHost: 'foobar.dev'});
var mw_target = function (req, res, next) {
targetHeaders = req.headers;
res.end();
};
proxyServer = createServer(3000, mw_proxy);
targetServer = createServer(8000, mw_target);
http.get('http://localhost:3000/api/', function (res) {
done();
});
});
afterEach(function () {
proxyServer.close();
targetServer.close();
});
it('should proxy host header to target server', function () {

@@ -131,11 +229,12 @@ expect(targetHeaders.host).to.equal('foobar.dev');

describe('Error handling', function () {
var proxyServer, targetServer;
var response;
beforeEach(function (done) {
servers = createServers({
proxy: proxyMiddleware('/api', {target:'http://localhost:666'}), // unreachable host on port:666
sourceMiddleware : function (req, res, next) {next()},
targetMiddleware: function (req, res, next) {next()},
});
var mw_proxy = proxyMiddleware('/api', {target:'http://localhost:666'}); // unreachable host on port:666
var mw_target = function (req, res, next) {next()};
proxyServer = createServer(3000, mw_proxy);
targetServer = createServer(8000, mw_target);
http.get('http://localhost:3000/api/', function (res) {

@@ -147,2 +246,8 @@ response = res;

afterEach(function () {
proxyServer.close();
targetServer.close();
});
it('should handle errors when host is not reachable', function () {

@@ -154,20 +259,21 @@ expect(response.statusCode).to.equal(500);

describe('Rewrite path', function () {
var proxyServer, targetServer;
var responseBody;
beforeEach(function (done) {
servers = createServers({
proxy: proxyMiddleware('/api', {
target:'http://localhost:8000',
pathRewrite: {
'^/api' : '/rest',
'^/remove' : '',
}
}),
sourceMiddleware : function (req, res, next) {next()},
targetMiddleware: function (req, res, next) {
res.write(req.url); // respond with req.url
res.end()
},
var mw_proxy = proxyMiddleware('/api', {
target:'http://localhost:8000',
pathRewrite: {
'^/api' : '/rest',
'^/remove' : ''
}
});
var mw_target = function (req, res, next) {
res.write(req.url); // respond with req.url
res.end()
};
proxyServer = createServer(3000, mw_proxy);
targetServer = createServer(8000, mw_target);
http.get('http://localhost:3000/api/foo/bar', function (res) {

@@ -181,3 +287,8 @@ res.on('data', function (chunk) {

it('should have response body with the rewritten req.url: "/rest/..."', function () {
afterEach(function () {
proxyServer.close();
targetServer.close();
});
it('should have rewritten path from "/api/foo/bar" to "/rest/foo/bar"', function () {
expect(responseBody).to.equal('/rest/foo/bar');

@@ -187,6 +298,2 @@ });

afterEach(function () {
closeServers(servers);
servers = null;
});

@@ -196,31 +303,12 @@ });

/**
* source Server: http:localhost:3000
* target Server: http:localhost:8000
**/
function createServers (options) {
var sourceServer,
targetServer;
function createServer (portNumber, middleware) {
var app = express();
// source server
var sourceApp = express();
sourceApp.use(options.sourceMiddleware);
sourceApp.use(options.proxy);
sourceServer = sourceApp.listen(3000);
if (middleware) {
app.use(middleware);
}
var server = app.listen(portNumber);
// target server
var targetApp = express();
targetApp.use(options.targetMiddleware);
targetServer = targetApp.listen(8000);
return {
sourceServer : sourceServer,
targetServer : targetServer,
}
return server;
}
function closeServers (servers) {
servers.sourceServer.close();
servers.targetServer.close();
}
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