Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/quantizor/middleware-proxy
↪ connect-compliant middleware for simple, transparent redirection of HTTP requests
https://github.com/quantizor/middleware-proxy
Last synced: 3 days ago
JSON representation
↪ connect-compliant middleware for simple, transparent redirection of HTTP requests
- Host: GitHub
- URL: https://github.com/quantizor/middleware-proxy
- Owner: quantizor
- Created: 2016-05-17T13:51:42.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-02T17:31:51.000Z (about 6 years ago)
- Last Synced: 2024-12-04T03:04:36.314Z (19 days ago)
- Language: JavaScript
- Homepage:
- Size: 16.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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
```Based on the work in [rgaskill/dev-rest-proxy](https://github.com/rgaskill/dev-rest-proxy)