Proxy angular request with node to service situated at azurewebsites

I need to make some proxy post request and get response from a target of this request. Here is my server.js file code: The problem that there isn't any response after proxy request. Here are parts of my server.js connected with proxy:

 var httpProxy = require('http-proxy');
 app.use(bodyParser.urlencoded({ extended: true }));

 var apiProxy = httpProxy.createProxyServer({
     secure: true,
     changeOrigin: true
 });

app.post('/v1/*', function(req, res) {
    apiProxy.web(req, res, { target: 
    'http://somesite.azurewebsites.net'});
    });
});

Currently I works at localhost:3000 and I have tested this proxy working with localhost:8888 (another local server) and it works perfect.

Also if I remove changeOrigin: true there is 404 response from azurewebsite "Page not exist". Service are workable (tested with postman).

Can't find the problem. Will be glad not only answers but even some additional sources about node&proxy. Thanks!

Answers:

Answer

I'm unfamiliar with http-proxy, but it's probably easier to use request when proxying some of the requests. You could do something similar with the following code:

var request = require('request');
var express = require('express');
var app = express();

app.use(bodyParser.urlencoded({ extended: true }));

app.post('/v1/*', function(req, res) {
    var newurl = 'http://somesite.azurewebsites.net';
    request(newurl).pipe(res);
});

app.listen(3000);

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.