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

koa2-proxy-middleware

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa2-proxy-middleware

a koa2 proxy middleware by means of http-proxy-middleware

  • 0.0.4
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Introduction

A koa2 middleware by means of http-proxy-middleware.

中文

Install

$ npm install --save-dev koa2-proxy-middleware

Usage

This is a very simple usage. If you are familiar with the use of http-proxy-middleware and path-to-regexp.

Example

const Koa = require('koa');
const proxy = require('koa2-proxy-middleware');
const bodyparser = require('koa-bodyparser');

const app = new Koa();

const options = {
  targets: {
    '/user': {
      // this is option of http-proxy-middleware
      target: 'http://localhost:3000', // target host
      changeOrigin: true, // needed for virtual hosted sites
    },
    '/user/:id': {
      target: 'http://localhost:3001',
      changeOrigin: true,
    },
    // (.*) means anything
    '/api/(.*)': {
      target: 'http://10.94.123.123:1234',
      changeOrigin: true,
      pathRewrite: {
        '/passager/xx': '/mPassenger/ee', // rewrite path
      }
    },
  }
}

app.use(proxy(options));


app.use(bodyparser({
  enableTypes:['json', 'form', 'text']
}));

options

{
  targets: {
    [key]: [option],
  },
}
key

The key route that you need to proxy.And through path-to-regexp into regexp. This is mean:

const regexp = pathToRegexp('route-key');
regexp.test(ctx.req.path);
option

The option corresponding to key and like http-proxy-middleware.

Tips

Bodyparser need to after proxy-middleware when request method is POST. Otherwise there will be a delay. I think this is a bug of bodyparser.

Keywords

FAQs

Package last updated on 18 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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