Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Proxy - Proxy a SSL localhost target from a basic http server

Tags:

node.js

proxy

ssl

What I am trying to do:

Proxy a java api that runs on https://127.0.0.1:443/api/ along side my UI that runs on non-SSL http://127.0.0.1:1337/ in order to circumnavigate some CORS issues.

My attempt:

  1. Proxy the api at the SSL port 443 to my non-SSL development port of 1338.
  2. proxy my UI to 1337
  3. Proxy 1137 to :8080/index.html and proxy 1338 to :8080/api/
  4. Access my app from localhost:8080

My problem:

The UI comes in just fine... but I can not hit the API at :8080/api/httpSession/init

Yes, I can still hit the API at https://localhost/api/httpSession/init

api.js - Renders index.html at :1337

var app = express();

app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  next();
});

var options = {
  changeOrigin: true,
  target: {
      https: true
  }
};

httpProxy.createServer(443, '127.0.0.1', options).listen(1338);

start.js - Proxies 1337 and 1338 into 8080

// First I start my two servers
uiServer.start(); // renders index.html at 1337
apiServer.start(); // 

// I attempt to patch them back into one single non-SSL port.
app
  .use('/', proxy({target: 'http://localhost:1337/'}))
  .all('/api/*', proxy({target: 'http://localhost:1338/'}))
  .listen(8080, function () {
    console.log('PROXY SERVER listening at http://localhost:%s', 8080);
  });
like image 621
Rick Bross Avatar asked Dec 27 '16 05:12

Rick Bross


People also ask

What is node proxy server?

node-http-proxy is an HTTP programmable proxying library that supports websockets. It is suitable for implementing components such as reverse proxies and load balancers.

Can I use localhost as proxy?

How to Proxy on Localhost. TLDR; Use a proxy against localhost by using your machine name or private ip address from ifconfig (or ipconfig).

What is an HTTP proxy?

An HTTP proxy acts as a high-performance content filter on traffic received by an HTTP client and HTTP server. The HTTP proxy protocol routes client requests from web browsers to the internet and supports rapid data caching.

What is a node proxy server?

Proxy server: Proxy servers act as intermediaries between the end-user and the website or API they wish to access. Why Proxy Server? In the standard method, the client sends a request directly to the API endpoint to fetch the desired data. In this case, the node.js proxy will act as an intermediary between the user and the API.

How do I install HTTP-proxy in Node JS?

Http-proxy is a one-liner node.js proxy middleware for connecting, expressing, and browser-sync. We can install them using the command below. npm install express npm install npm install --save-dev http-proxy-middleware Running the command above adds them as dependencies to the package.json file below.

Do I need Express to use HTTP-proxy?

The http-proxy package doesn't require you to use Express. You can also use Node's built-in HTTPServer class: With a proxy server, there's two HTTP requests: the inbound request that the proxy server received, and the outbound request that the proxy server sends. In the previous examples, the inbound request is the same as the outbound request.

What is a proxy server?

Proxy server: Proxy servers act as intermediaries between the end-user and the website or API they wish to access. Why Proxy Server? In the standard method, the client sends a request directly to the API endpoint to fetch the desired data.


1 Answers

What you're looking for is request piping. Try this example:

  // Make sure request is in your package.json
  //   if not, npm install --save request
  var request = require('request');

  // Intercept all routes to /api/...
  app.all('/api/*', function (req, res) {
    // Get the original url, it's a fully qualified path
    var apiPath = req.originalUrl;

    // Form the proxied URL to your java API
    var url = 'https://127.0.0.1' + apiPath;

    // Fire off the request, and pipe the response
    // to the res handler
    request.get(url).pipe(res);
  });

Make sure to add some error handling if the api can't be reached, such as this SO solution.

like image 162
Jack Wade Avatar answered Oct 20 '22 00:10

Jack Wade