HTTP Proxies

Dr. Greg Bernstein

April 19th, 2020

HTTP Proxies

Proxies

  • IETF Definition
    • An intermediary program which acts as both a server and a client for the purpose of making requests on behalf of other clients.

Proxy Types

From Wikipedia Proxy Server

  • A forward proxy is an Internet-facing proxy used to retrieve data from a wide range of sources. Used for monitoring, content filtering, bypassing filters and censorship, caching, and more.

  • A reverse proxy is an internal-facing proxy used as a front-end to control access to servers on a private network. Common tasks include: load-balancing, authentication, decryption or caching.

Proxies in Development

We will want to use proxy functionality in development:

Proxies in Development 2

  • We will write our own application server to give and process data from our web app.

  • We will combine our bundler (Parcel.js) with and Express.js server and additional middleware.

Proxies in Development 3

Need Specifics on Addresses and Ports

Proxies with Node/Express

Proxy Middleware!

const express = require('express');
const proxy = require('http-proxy-middleware');
const app = express();
// Paths we want to forward to the app server
const forward = ['/tours', '/login', '/logout'];
app.use(forward, proxy({target: 'http://127.0.0.11:1711'}));

Using Parcel Bundler in Express

const Bundler = require('parcel-bundler');
const app = express();
// Instance of the parcel.js bundler with start file
const bundler = new Bundler('./index.html');
app.use(bundler.middleware());

Proxy with Express and Parcel

Putting it all together, based on StackOverflow post

// Need Local install of parcel-bundler so we and import it below.
const Bundler = require('parcel-bundler');
const express = require('express');
const {createProxyMiddleware} = require('http-proxy-middleware');
const app = express();
// Paths we want to forward to the app server
const forward = ['/activities', '/login', '/logout', '/users'];
app.use(forward, createProxyMiddleware({target: 'http://127.0.0.11:1711'}));
// Instance of the parcel.js bundler with start file
const bundler = new Bundler('./index.html');
app.use(bundler.middleware());
app.listen(1234);

Usage Notes

  • Need to run development/proxy with Node.js in a terminal
  • Need to run your application server in another terminal
  • All ports and addresses need to be correctly matched!
  • Getting a lot of 404s? Check what URLs actually sent to app server!