Giter Club home page Giter Club logo

koa-proxy's Introduction

koa-proxy is been transfered to edorivai

koa-proxy Build Status Coverage Status

Proxy middleware for koa


Install

$ npm install koa-proxy -S

Usage

When you request http://localhost:3000/index.js, it will fetch http://alicdn.com/index.js and return.

var koa = require('koa');
var proxy = require('koa-proxy');
var app = koa();
app.use(proxy({
  host: 'http://alicdn.com'
}));
app.listen(3000);

You can proxy a specified url.

app.get('index.js', proxy({
  url: 'http://alicdn.com/index.js'
}));

You can specify a key/value object that can map your request's path to the other.

app.get('index.js', proxy({
  host: 'http://alicdn.com',
  map: {
    'index.js': 'index-1.js'
  }
}));

You can specify a function that can map your request's path to the desired destination.

app.get('index.js', proxy({
  host: 'http://alicdn.com',
  map: function(path) { return 'public/' + path; }
}));

You can specify match criteria to restrict proxy calls to a given path.

app.use(proxy({
  host:  'http://alicdn.com', // proxy alicdn.com...
  match: /^\/static\//        // ...just the /static folder
}));

Or you can use match to exclude a specific path.

app.use(proxy({
  host:  'http://alicdn.com',     // proxy alicdn.com...
  match: /^(?!\/dontproxy\.html)/ // ...everything except /dontproxy.html
}));

Proxy won't send cookie to real server, you can set jar = true to send it.

app.use(proxy({
  jar: true,
}));

Proxy won't send 'foo' and 'bar' headers to real server, or recieve 'jar-jar' from real server.

app.use(proxy({
  suppressRequestHeaders: ['foo','bar'], // case-insensitive
  suppressResponseHeaders: ['jar-jar'] // case-insensitive
}));

LICENSE

Copyright (c) 2014 popomore. Licensed under the MIT license.

koa-proxy's People

Contributors

andreasrs avatar capaj avatar chrisveness avatar greengremlin avatar greim avatar kevinmook avatar lizell avatar mehiel avatar popomore avatar sdd avatar tomaash avatar yugloocamai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

koa-proxy's Issues

没有返回statusCode

没有返回statusCode 导致 代理请求的文件根本就没有返回的也是200

this.status = res.statusCode;  //新加上这句就OK了
this.body = res.body;

How to proxy app.get() in Koa v2?

I am trying to use koa-proxy with koa v2. The

app.use(convert(proxy({host: ..., match: ...})) works fine

Now I want to map one url to a specific proxy url. The docs talk about app.get() but since Koa 2 doesn't have an app.get() function anymore I figured I have to use koa-router...

Using koa-router@next, I've tried this:

router.get('/one', proxy({
   host: 'http://localhost:7777',
   map: function(path) { return '/something/' + path }
}))

router.get('/two', proxy({url: 'http://localhost:7777'}))

app
  .use(router.routes())
  .use(router.allowedMethods())

Calling /one or /two is logged in my Koa server but nothing is passed to the proxy. What am I missing?

SSE proxy not working

Im trying to proxy sse requests, but it looks like it just waits for response 2 minutes and then fails with net::ERR_EMPTY_RESPONSE.

2017-06-06 11 43 04

Need help with maintenance?

Hey man, thanks for writing this useful little library. I've been noticing that issues and PR's have been lying around for some time. I just wanted to reach out and offer a hand maintaining this lib, if you're interested.

Cheers!

SSL is not supported

since the host in the headers is not properly rewritten SSL verification fails.

Option to exclude url or path

Would be great if you could pass in an options.exclude so you can proxy everything except an url or path. Maybe allow a string, array or regular expression.

For now, I've worked around this with the following hack:

var proxyMiddleware = proxy({
    host: proxyHost
});
app.use(function* (next) {
    // Do not route anything related to this word
    if (this.path.indexOf(excludedWord) !== -1) {
        return;
    }
    yield* proxyMiddleware.call(this, next);
});

cookie默认被缓存

很赞的koa代理模块!
使用过程中发现莫名其妙的cookie问题,甚至都怀疑浏览器有bug了囧
调试了好久,原来有一个默认选项jar是true:

var request = coRequest.defaults({ jar: typeof options.jar === 'undefined' ? true : options.jar });

一开始不知道什么鬼,后来一层层看进去,最终在request模块里找到这个配置项https://github.com/request/request#requestoptions-callback
如果为true,则会缓存cookie
不知为何本模块要把这个jar选项默认为true,至少文档里提一下有jar这个配置吧

debug info

I miss some debug info ex.

app.use(proxy({
  host,
  match,
  debug: true,
});

and for each request I would like some logs:

Redirecting to POST http://foo-bar.com, waiting for response
Got answer from POST http://foo-bar.com, 201 status
No match, 404 status

jar defaults

Hi,

TLDR;
Maybe jar = false by default is a better decision?


It seems to me jar = true is not such a good default value.
Last time I spent plenty of hours while debugging our app, using wireshark / tcpdump on servers and making binary search on code because I was ran out of any ideas.

koa-proxy is for node.js. And usually set-cookie header contains user-grained cookies, such as csrftoken, user_id and so on. By setting jar = true by default you implicitly save cookies for user A and then send them together with cookies from user B. This is weird to me.

I will be very happy if I would be the latest who completed such a quest understanding what is happening.

如何为站点某个目录做代理,另:koa里没有app.get呀

我想实现http://localhost:3000/proxy => http://10.2.xx.xx/
这要什么弄
另:
readme的例子,
`
var koa = require('koa');
var proxy = require('koa-proxy');
var app = koa();
app.use(proxy({
host: 'http://alicdn.com'
}));
app.listen(3000);

app.get('index.js', proxy({
url: 'http://alicdn.com/index.js'
}));
app.get('index.js', proxy({
host: 'http://alicdn.com',
map: {
'index.js': 'index-1.js'
}
}));
`
app.get 这样对吗,koa也没有这个方法呀。。。why

Cookies are retained across requests

There needs to be a way to disable the request cookie jar. When running selenium tests koa-proxy retains cookies across browser sessions. Even clearing cookies does not stop koa-proxy from forwarding the cookies. Please expose the jar setting to make it easier to disable this behavior.

Breaks on JSON bodies

FOr JSON requests, on POST and PUT request is throwing:

'Argument error, options.body.'

If I do this before control reaches the proxy, the error goes away:

this.request.body = JSON.stringify(this.body);

cookie默认被缓存

使用过程中发现莫名其妙的cookie问题,甚至都怀疑浏览器有bug了囧
调试了好久,原来有一个默认选项jar是true:

var request = coRequest.defaults({ jar: typeof options.jar === 'undefined' ? true : options.jar });

一开始不知道什么鬼,后来一层层看进去,最终在request模块里找到这个配置项https://github.com/request/request#requestoptions-callback
如果为true,则会缓存cookie
不知为何本模块要把这个jar选项默认为true,至少文档里提一下有jar这个配置吧

Support options of request

I use koa-proxy to proxy some https website, but the certificate has expired, so koa-proxy error on [Error: certificate revoked]. But I found co-request options to set
https://github.com/request/request#requestoptions-callback = false, can ignore this error.

Change code like this: options || (options = {jar: true}); var request = coRequest.defaults(options); to support request options, is it Ok?

Tagging a new release, timeplan?

It's been a while since a release has been made. Some nice features have been introduced since.

Do you have a plan for when the next tag will be created?

memory leaks?

screen shot 2016-06-21 at 12 07 44

I'm operating a simple proxy server using koa-proxy & koa-vhost, and found it's leaking.
The HTTP request frequency is around 100req/min, 99% of them is to the proxy.

It crashed due to out of memory (the server has memory quota) and seems that it failed to allocate memory in request.js.
here is a log:

2016-06-20T10:46:13.82+0900 [App/0]      ERR [App:inkdrop-web][Err] Mon, 20 Jun 2016 01:46:13 GMT koa CouchDB proxy: GET (49673027bcb07268cc6ce879043362c4) /
2016-06-20T10:46:14.76+0900 [App/0]      ERR [App:inkdrop-web][Err] FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
2016-06-20T10:46:14.76+0900 [App/0]      OUT <--- Last few GCs --->
2016-06-20T10:46:14.76+0900 [App/0]      OUT 40563324 ms: Scavenge 97.6 (137.1) -> 97.6 (137.1) MB, 1.4 / 0 ms (+ 246.9 ms in 1 steps since last GC) [allocation failure] [incremental marking delaying mark-sweep].
2016-06-20T10:46:14.76+0900 [App/0]      OUT 40563895 ms: Mark-sweep 96.1 (137.1) -> 96.1 (137.1) MB, 337.0 / 0 ms [last resort gc].
2016-06-20T10:46:14.76+0900 [App/0]      OUT <--- JS stacktrace --->
2016-06-20T10:46:14.76+0900 [App/0]      OUT ==== JS stack trace =========================================
2016-06-20T10:46:14.76+0900 [App/0]      OUT Security context: 0x1f08a8fe3ac1 <JS Object>
2016-06-20T10:46:14.76+0900 [App/0]      OUT     2: new constructor(aka Request) [/home/vcap/app/node_modules/request/request.js:142] [pc=0x3af3468a6f73] (this=0x1c549e219cd1 <a Request with map 0x16f08ae96179>,options=0x1c549e219c99 <an Object with map 0x16f08ae96a69>)
2016-06-20T10:46:14.76+0900 [App/0]      OUT     4: request(aka request) [/home/vcap/app/node_modules/request/index.js:~44] [pc=0x3af345973e3b] (this=0x1f08a8f04189 <undefined>,uri=0x1c549e219e11 <an Object with map ...
2016-06-20T10:46:15.47+0900 [RTR/1]      OUT black.inkdrop.info:443 - [20/06/2016:01:46:13 +0000] "GET / HTTP/1.1" 502 0 67 "-" "-" 108.168.250.154:19593 x_forwarded_for:"218.221.57.237" x_forwarded_proto:"https" vcap_request_id:28fd427a-5d4d-48b4-737c-53c29583a5d6 response_time:1.648581046 app_id:bcefd8a4-6c44-44c2-9d81-617ef7cfe172 x_global_transacti
on_id:"2837575319"
2016-06-20T10:46:15.46+0900 [RTR/1]      OUT black.inkdrop.info:443 - [20/06/2016:01:46:13 +0000] "GET / HTTP/1.1" 502 0 67 "-" "-" 108.168.250.156:54532 x_forwarded_for:"218.221.57.237" x_forwarded_proto:"https" vcap_request_id:a48794ae-4f5f-45cf-7455-0b31a6839363 response_time:1.626705701 app_id:bcefd8a4-6c44-44c2-9d81-617ef7cfe172 x_global_transacti
on_id:"656538031"
2016-06-20T10:46:16.04+0900 [RTR/2]      OUT black.inkdrop.info:443 - [20/06/2016:01:46:16 +0000] "GET /user-49673027bcb07268cc6ce879043362c4/_local/GOTCLuWYJqPyyqyexz2U2A%3D%3D? HTTP/1.1" 502 0 67 "-" "-" 108.168.250.155:29097 x_forwarded_for:"218.221.57.237" x_forwarded_proto:"https" vcap_request_id:b8fcd42e-0b50-440e-4e73-c7646b5516dc response_time:
0.008900185 app_id:bcefd8a4-6c44-44c2-9d81-617ef7cfe172 x_global_transaction_id:"656540079"
2016-06-20T10:46:16.22+0900 [RTR/3]      OUT black.inkdrop.info:443 - [20/06/2016:01:46:16 +0000] "GET /user-49673027bcb07268cc6ce879043362c4/_local/C3WGRxHM3iD.iN6Lsj9MuA%3D%3D? HTTP/1.1" 502 0 67 "-" "-" 108.168.250.152:58570 x_forwarded_for:"218.221.57.237" x_forwarded_proto:"https" vcap_request_id:9586d5a0-c7d4-4435-6d0d-8c6081f1722a response_time:
0.00910367 app_id:bcefd8a4-6c44-44c2-9d81-617ef7cfe172 x_global_transaction_id:"3498664903"
2016-06-20T10:46:17.64+0900 [App/0]      ERR [App:inkdrop-web][Err] Mon, 20 Jun 2016 01:46:17 GMT koa NODE_ENV: production
2016-06-20T10:46:18.17+0900 [App/0]      ERR [App:inkdrop-web][Err] Mon, 20 Jun 2016 01:46:18 GMT koa Loading configuration for env production

any help will be appreciated.

怎样和gulp结合来使用?

现在我想在我的gulp应用中使用 koa-proxy,应该在 gulpfile 里面怎样写?

var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();

var path = require('path');
var gls = require('gulp-live-server');

var server = gls.static('static', 1989);

gulp.task(~~~);

感谢 :)

headers flag is wrong

var opt = {
  url: url + '?' + this.querystring,
  header: this.header,
  encoding: null
};

should be headers: this.header,

Documentation issue about "jar"

It is written: "Proxy won't send cookie to real server, you can set jar = true to send it."
That's not the appropriate meaning of the jar option of request.
In request documentation, it states:
jar - if true, remember cookies for future use (or define your custom cookie jar; see examples section)

So, if jar=false, the cookies are still transmitted to the real server, and won't be cached by the proxy.

如何使用多个 host

因为我的项目需要调用多个 api, 请问如何配置多个 代理,

development: (config) => ({
    compiler_public_path: `http://${config.server_host}:${config.server_port}/`,
    proxy: {
      enabled: true,
      options: {
        host: 'http://192.168.8.14:1338',
        match: /^\/api\/.*/,
      },
      option2: {
        host: 'http://apiv2.muzhifm.com',
        match: /^\/muzhifmApi\/.*/,
        map: (path) => {
          console.log(path.replace('muzhifmApi', ''));
          return path.replace('/muzhifmApi', '');
        },
      },
    },
  }),

我的代码是这样的,可是第二个不能成功.

if (config.proxy && config.proxy.enabled) {
  app.use(convert(proxy(config.proxy.options)));
  app.use(convert(proxy(config.proxy.option2)));
}

Failed when the response is image/jpge

Code:
<img name="randImage" id="randImage" ng-src="image.jsp" width="80" height="40" border="1" >
When I tried to get the captcha image from the server, it returns nothing with content type 'html/text' which should be 'image/jpge' instead.

TS Support

Provide TS definitions for this module please.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.