# middleware-proxy

> A connect-compliant middleware for simple, transparent redirection of HTTP requests.

Latest version **2.0.5** (published 2018-10-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install middleware-proxy
pnpm add middleware-proxy
yarn add middleware-proxy
bun add middleware-proxy
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2018-10-02 |
| First published | 2016-05-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 5 |
| Dependencies | 1 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Evan Jacobs |
| Maintainers | probablyup |
| Keywords | http, proxy, server, connect, express, middleware |

## Links

- npm: https://www.npmjs.com/package/middleware-proxy
- Repository: https://github.com/probablyup/middleware-proxy
- npm.io page: https://npm.io/package/middleware-proxy

## Dependencies (1)

- [request](https://npm.io/package/request.md) ^2.72.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 2.0.5 (latest) — 2018-10-02
- 2.0.4 — 2018-09-24
- 2.0.3 — 2018-09-14
- 2.0.2 — 2016-05-20
- 2.0.1 — 2016-05-20
- 2.0.0 — 2016-05-20
- 1.0.0 — 2016-05-17

## README

# middleware-proxy

This is a simple tool that smooths the difficulties of working on integrated projects where the backend is hosted elsewhere from where you are developing your frontend. The proxy allows you to access your static HTML through a node server and redirect your RESTful calls to the implementing server.

## usage

```js
const proxy = require('middleware-proxy');

/**
 * Returns a middleware function that selectively proxies incoming requests to another server.
 *
 * @param  {string|RegExp} matcher - used to detect if the middleware should be applied against the request URL
 * @param  {string} server - a fully qualified server address, e.g. http://localhost:8080
 * @param  {string} [path_to_strip] - (optional) a leading string that should be removed from the request URI
 *
 * @return {function} middleware, suitable for use in express or connect
 */
proxy('/api/v1', 'http://localhost:8080')`
```

### basic express example

```js
const express = require('express');
const app = express();
const http = require('http');
const proxy = require('middleware-proxy');

app.configure(function() {
    app.set('port', process.env.PORT || 3000);
    app.use(express.static(__dirname + '/app'));
});

app.use(proxy('/service', 'http://localhost:8080'));

http.createServer(app).listen(app.get('port'), function() {
    console.log(`Express server listening on port ${app.get('port')}`);
});
```

This will send all requests starting with `/service` to `http://localhost:8080/service`.

### using [budo](https://www.npmjs.com/package/budo) (a browserify livereload solution)

```js
const proxy = require('middleware-proxy');

require('budo')('index.js', {
    browserify: {
        debug: true,
    },

    host: '0.0.0.0',
    live: true,
    open: true,
    portfind: true,
    pushstate: true,
    serve: 'assets/bundle.js',
    stream: process.stdout,

    middleware: [proxy('/service', 'http://localhost:8080')],
});

```

## removing an API prefix

Let's say your integration environment has chosen `/api` as the mount point for communicating with your application's backend APIs. However, you're building your frontend in a separate project and can't easily run a dev server that joins both functionalities. `middleware-proxy` solves this issue by allowing a third optional parameter that defines the part of the request URL to be stripped away when proxying the request to your destination host.

Assuming your remote development API host is at `http://localhost:8080` and has a RESTful collection called `/posts`, you would write your AJAX calls to `GET` from `/api/posts` and use the following configuration:

```js
proxy('/api/posts', 'http://localhost:8080', '/api');
```

The request is transparently redirected behind the scenes:

```
GET /api/posts -> GET http://localhost:8080/posts
```

<sub>Based on the work in [rgaskill/dev-rest-proxy](https://github.com/rgaskill/dev-rest-proxy)</sub>

---
_Source: https://npm.io/package/middleware-proxy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
