SkiGeek SkiGeek - 2 months ago 20
AngularJS Question

.NET equivalent to node http-proxy

I am looking for a .NET solution to proxy API requests between a client application and a rest API. One solution that requires node.js back end I found is node http-proxy following this http://shawnsimondeveloper.com/nodeproxyangular/ example.

The .NET proxy needs to be able to attach a certificate to the api requests for authentication. The proxy should take any request and pass it through to the destination URL.

What options are available for .NET?

Answer

I decided to stick with the node solution, here is the simple code used to proxy a request with a certificate to the API. The key was to add the key, cert, and ca properties to the target in the proxy options.

'use strict';
let fs = require('fs');
let https = require('https');
let express = require('express');
let httpProxy = require('http-proxy');
let privateKey = fs.readFileSync(__dirname + '/certs/server.key', 'utf8');
let certificate = fs.readFileSync(__dirname + '/certs/cert.pem', 'utf8');
let caChain = fs.readFileSync(__dirname + '/certs/cachain.pem', 'utf8');
let staticRoot = __dirname + '/';

let httpsOptions = {
    key: privateKey,
    cert: certificate,
    ca: [caChain]
};

let proxyOptions = {
    target: {
        host: 'api.host.com',
        protocol: 'https:',
        key: privateKey,
        cert: certificate,
        ca: caChain,
    },
    changeOrigin: true
};

let app = express();
let apiProxy = httpProxy.createProxyServer(proxyOptions);
app.use(express.static(staticRoot));
app.all('/api/*', function(req, res) {
    console.log(Date().toString('yyyymmdd hh:mm:ss') + ' - API Request Proxied');
    apiProxy.web(req, res, proxyOptions);
});

https.createServer(httpsOptions, app).listen(3001, function(){
    console.log('https server listening on port 3001');
});