http-proxy-rules не обслуживающий изображения и другие ресурсы - PullRequest
0 голосов
/ 09 мая 2018
  1. У меня есть приложение узла js (порт 10002) и приложение angular-js (порт 10003), развернутое с использованием pm2.Все хорошо, когда вы указываете порт, используя двоеточие (:), например, host: 10003
  2. . По какой-то причине мне пришлось создать прокси-узел nodejs с использованием http-proxy и http-proxy-rules для пересылки от 10001 до 10002 ИЛИ 10003 следующим образом.:

var http = require('http'),
httpProxy = require('http-proxy'),
HttpProxyRules = require('http-proxy-rules');

// Set up proxy rules instance
var proxyRules = new HttpProxyRules({
rules: {
'.*/api': 'http://localhost:10002/api', // Rule (1)
'.*/': 'http://localhost:10003/' // Rule (2)
},
default: 'http://localhost:10003' // default target
});

// Create reverse proxy instance
var proxy = httpProxy.createProxy();

// Create http server that leverages reverse proxy instance
// and proxy rules to proxy requests to different targets
http.createServer(function(req, res) {

// a match method is exposed on the proxy rules instance
// to test a request to see if it matches against one of the specified rules
console.log(req.url);
var target = proxyRules.match(req);
if (target) {
return proxy.web(req, res, {
  target: target
});
}

res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('The request url and path did not match any of the listed rules!');
}).listen(process.env.PROXYPORT || 10001);

Как видите, я скопировал точно такой же код из https://www.npmjs.com/package/http-proxy-rules

Проблема:

Когда доступ к приложению Angular JS осуществляется с помощьюФактический порт, как http://host:10003, Я могу видеть изображения.Но когда я использую прокси-порт http://host:10001,, который приводит меня к порту 10003, , все работает, кроме статических изображений, CSS и т. Д.

Пожалуйста, оцените любую помощь по этому вопросу.

1 Ответ

0 голосов
/ 10 мая 2018

Это была глупая ошибка, если кто-то столкнулся с той же проблемой, вот решение, удалите. * Из фильтра следующим образом:

// Set up proxy rules instance
var proxyRules = new HttpProxyRules({
rules: {
'/api': 'http://localhost:10002/api', // Rule (1)
'/': 'http://localhost:10003/' // Rule (2)
},
default: 'http://localhost:10003' // default target
});
...