Google Cloud Functions HTTP Proxy Relay with http-proxy-middleware

  • infoFull Post Details
    info_outlineClick for Full Post Details
    Date Posted:
    Nov. 24, 2019
    Last Updated:
    Nov. 24, 2019
  • classTags
    classClick for Tags
    Tags:

Quick post: I just want to say that I was (pleasantly) surprised to find how easy it was to setup and use a NodeJS powered HTTP request proxy, with Google Cloud Functions and “http-proxy-middleware” for Express JS. For example, here is a bare-bones setup:

Index.js:

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

const app = express();
app.all('*', getProxy());

function getProxy() {
	return proxy({
		target: 'https://postb.in/123-456',
		changeOrigin: true,
		followRedirects: true,
		secure: true
	});
}

module.exports = {
	proxy: app
}

Package.json:

{
	"name": "gcp-proxy-func",
	"version": "1.0.0",
	"description": "Run http-proxy-middleware to relay requests",
	"main": "index.js",
	"scripts": {
		"deploy": "gcloud functions deploy proxy --runtime nodejs8 --trigger-http --memory=128 --timeout=60s"
	},
	"dependencies": {
		"express": "^4.17.1",
		"http-proxy-middleware": "^0.20.0"
	}
}

Deploy command:

gcloud functions deploy proxy --runtime nodejs8 --trigger-http

Note:

Although this didn’t quite end up fulfilling the need that I had for a project, I thought it was neat how fast the function could spin up, respond to a request, proxy it, and respond.

Leave a Reply

Your email address will not be published. Required fields are marked *