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:
:8080/index.html
and proxy 1338 to :8080/api/
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);
});
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.
How to Proxy on Localhost. TLDR; Use a proxy against localhost by using your machine name or private ip address from ifconfig (or ipconfig).
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With