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

refactor: migrate to typescript #328

Merged
merged 2 commits into from Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
2 changes: 2 additions & 0 deletions .prettierignore
@@ -0,0 +1,2 @@
dist
coverage
2 changes: 1 addition & 1 deletion .prettierrc
@@ -1,5 +1,5 @@
{
"tabWidth": 2,
"semi": false,
"semi": true,
"singleQuote": true
}
18 changes: 15 additions & 3 deletions .travis.yml
@@ -1,12 +1,24 @@
sudo: false
language: node_js
node_js:
- 'stable'
- '10'
- '9'
- '8'
- '7'
- '6'
after_success: npm run coveralls
before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.15.2
- export PATH=$HOME/.yarn/bin:$PATH
cache: yarn
stages:
- lint
- test
- coveralls
jobs:
include:
- stage: lint
script: yarn lint
- stage: coveralls
script: yarn coveralls
env:
global:
# COVERALLS_REPO_TOKEN=
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog

## next

- refactor: migrate to typescript ([#328](https://github.com/chimurai/http-proxy-middleware/pull/328))
- feat(middleware): Promise / async support ([#328](https://github.com/chimurai/http-proxy-middleware/pull/328/files#diff-7890bfeb41abb0fc0ef2670749c84077R50))

## [v0.19.1](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.19.1)

- fix(log): handle case when error code is missing ([#303](https://github.com/chimurai/http-proxy-middleware/pull/303))
Expand Down
106 changes: 58 additions & 48 deletions README.md
Expand Up @@ -3,7 +3,7 @@
[![Build Status](https://img.shields.io/travis/chimurai/http-proxy-middleware/master.svg?style=flat-square)](https://travis-ci.org/chimurai/http-proxy-middleware)
[![Coveralls](https://img.shields.io/coveralls/chimurai/http-proxy-middleware.svg?style=flat-square)](https://coveralls.io/r/chimurai/http-proxy-middleware)
[![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)
[![dependency Status](https://snyk.io/test/npm/http-proxy-middleware/badge.svg)](https://snyk.io/test/npm/http-proxy-middleware)
[![dependency Status](https://snyk.io/test/npm/http-proxy-middleware/badge.svg?style=flat-square)](https://snyk.io/test/npm/http-proxy-middleware)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)

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).
Expand All @@ -15,13 +15,16 @@ Powered by the popular Nodejitsu [`http-proxy`](https://github.com/nodejitsu/nod
Proxy `/api` requests to `http://www.example.org`

```javascript
var express = require('express')
var proxy = require('http-proxy-middleware')
var express = require('express');
var proxy = require('http-proxy-middleware');

var app = express()
var app = express();

app.use('/api', proxy({ target: 'http://www.example.org', changeOrigin: true }))
app.listen(3000)
app.use(
'/api',
proxy({ target: 'http://www.example.org', changeOrigin: true })
);
app.listen(3000);

// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
```
Expand Down Expand Up @@ -68,9 +71,9 @@ Proxy middleware configuration.
#### proxy([context,] config)

```javascript
var proxy = require('http-proxy-middleware')
var proxy = require('http-proxy-middleware');

var apiProxy = proxy('/api', { target: 'http://www.example.org' })
var apiProxy = proxy('/api', { target: 'http://www.example.org' });
// \____/ \_____________________________/
// | |
// context options
Expand All @@ -88,7 +91,7 @@ var apiProxy = proxy('/api', { target: 'http://www.example.org' })

```javascript
// shorthand syntax for the example above:
var apiProxy = proxy('http://www.example.org/api')
var apiProxy = proxy('http://www.example.org/api');
```

More about the [shorthand configuration](#shorthand).
Expand All @@ -99,8 +102,8 @@ An example with `express` server.

```javascript
// include dependencies
var express = require('express')
var proxy = require('http-proxy-middleware')
var express = require('express');
var proxy = require('http-proxy-middleware');

// proxy middleware options
var options = {
Expand All @@ -116,15 +119,15 @@ var options = {
// override target 'http://www.example.org' to 'http://localhost:8000'
'dev.localhost:3000': 'http://localhost:8000'
}
}
};

// create the proxy (without context)
var exampleProxy = proxy(options)
var exampleProxy = proxy(options);

// mount `exampleProxy` in web server
var app = express()
app.use('/api', exampleProxy)
app.listen(3000)
var app = express();
app.use('/api', exampleProxy);
app.listen(3000);
```

## Context matching
Expand Down Expand Up @@ -172,10 +175,10 @@ Providing an alternative way to decide which requests should be proxied; In case
* @return {Boolean}
*/
var filter = function(pathname, req) {
return pathname.match('^/api') && req.method === 'GET'
}
return pathname.match('^/api') && req.method === 'GET';
};

var apiProxy = proxy(filter, { target: 'http://www.example.org' })
var apiProxy = proxy(filter, { target: 'http://www.example.org' });
```

## Options
Expand Down Expand Up @@ -224,23 +227,23 @@ Providing an alternative way to decide which requests should be proxied; In case
// simple replace
function logProvider(provider) {
// replace the default console log provider.
return require('winston')
return require('winston');
}
```

```javascript
// verbose replacement
function logProvider(provider) {
var logger = new (require('winston')).Logger()
var logger = new (require('winston')).Logger();

var myCustomProvider = {
log: logger.log,
debug: logger.debug,
info: logger.info,
warn: logger.warn,
error: logger.error
}
return myCustomProvider
};
return myCustomProvider;
}
```

Expand All @@ -257,19 +260,19 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
function onError(err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
})
});
res.end(
'Something went wrong. And we are reporting a custom error message.'
)
);
}
```

- **option.onProxyRes**: function, subscribe to http-proxy's `proxyRes` event.

```javascript
function onProxyRes(proxyRes, req, res) {
proxyRes.headers['x-added'] = 'foobar' // add new header to response
delete proxyRes.headers['x-removed'] // remove header from response
proxyRes.headers['x-added'] = 'foobar'; // add new header to response
delete proxyRes.headers['x-removed']; // remove header from response
}
```

Expand All @@ -278,7 +281,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
```javascript
function onProxyReq(proxyReq, req, res) {
// add custom header to request
proxyReq.setHeader('x-added', 'foobar')
proxyReq.setHeader('x-added', 'foobar');
// or log the req
}
```
Expand All @@ -288,7 +291,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
```javascript
function onProxyReqWs(proxyReq, req, socket, options, head) {
// add custom header
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar')
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
}
```

Expand All @@ -297,15 +300,15 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
```javascript
function onOpen(proxySocket) {
// listen for messages coming FROM the target here
proxySocket.on('data', hybiParseAndLogMessage)
proxySocket.on('data', hybiParseAndLogMessage);
}
```

- **option.onClose**: function, subscribe to http-proxy's `close` event.
```javascript
function onClose(res, socket, head) {
// view disconnected websocket connections
console.log('Client disconnected')
console.log('Client disconnected');
}
```

Expand Down Expand Up @@ -383,13 +386,13 @@ The following options are provided by the underlying [http-proxy](https://github
Use the shorthand syntax when verbose configuration is not needed. The `context` and `option.target` will be automatically configured when shorthand is used. Options can still be used if needed.

```javascript
proxy('http://www.example.org:8000/api')
proxy('http://www.example.org:8000/api');
// proxy('/api', {target: 'http://www.example.org:8000'});

proxy('http://www.example.org:8000/api/books/*/**.json')
proxy('http://www.example.org:8000/api/books/*/**.json');
// proxy('/api/books/*/**.json', {target: 'http://www.example.org:8000'});

proxy('http://www.example.org:8000/api', { changeOrigin: true })
proxy('http://www.example.org:8000/api', { changeOrigin: true });
// proxy('/api', {target: 'http://www.example.org:8000', changeOrigin: true});
```

Expand All @@ -399,7 +402,10 @@ If you want to use the server's `app.use` `path` parameter to match requests;
Create and mount the proxy without the http-proxy-middleware `context` parameter:

```javascript
app.use('/api', proxy({ target: 'http://www.example.org', changeOrigin: true }))
app.use(
'/api',
proxy({ target: 'http://www.example.org', changeOrigin: true })
);
```

`app.use` documentation:
Expand All @@ -411,27 +417,27 @@ app.use('/api', proxy({ target: 'http://www.example.org', changeOrigin: true }))

```javascript
// verbose api
proxy('/', { target: 'http://echo.websocket.org', ws: true })
proxy('/', { target: 'http://echo.websocket.org', ws: true });

// shorthand
proxy('http://echo.websocket.org', { ws: true })
proxy('http://echo.websocket.org', { ws: true });

// shorter shorthand
proxy('ws://echo.websocket.org')
proxy('ws://echo.websocket.org');
```

### External WebSocket upgrade

In the previous WebSocket examples, http-proxy-middleware relies on a initial http request in order to listen to the http `upgrade` event. If you need to proxy WebSockets without the initial http request, you can subscribe to the server's http `upgrade` event manually.

```javascript
var wsProxy = proxy('ws://echo.websocket.org', { changeOrigin: true })
var wsProxy = proxy('ws://echo.websocket.org', { changeOrigin: true });

var app = express()
app.use(wsProxy)
var app = express();
app.use(wsProxy);

var server = app.listen(3000)
server.on('upgrade', wsProxy.upgrade) // <-- subscribe to http 'upgrade'
var server = app.listen(3000);
server.on('upgrade', wsProxy.upgrade); // <-- subscribe to http 'upgrade'
```

## Working examples
Expand Down Expand Up @@ -468,16 +474,20 @@ Run the test suite:

```bash
# install dependencies
$ npm install
$ yarn

# linting
$ npm run lint
$ yarn lint
$ yarn lint:fix

# building (compile typescript to js)
$ yarn build

# unit tests
$ npm test
$ yarn test

# code coverage
$ npm run cover
$ yarn cover
```

## Changelog
Expand All @@ -488,4 +498,4 @@ $ npm run cover

The MIT License (MIT)

Copyright (c) 2015-2018 Steven Chim
Copyright (c) 2015-2019 Steven Chim