这个问题,需要改一下 angular 源码,我是这样解决的。
用代理
var http = require('http'), httpProxy = require('http-proxy');
//
// Create your proxy server and set the target in the options.
//
var proxy_front = httpProxy.createProxyServer({});
var proxy_end = httpProxy.createProxyServer({});
// var proxy
// =httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8000);
// 捕获异常
proxy_front.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain' });
res.end('Front Server:Something went wrong'+err);
});
proxy_end.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain' });
res.end('App Server:Something went wrong'+err);
});
// 另外新建一个 HTTP 80 端口的服务器,也就是常规 Node 创建 HTTP 服务器的方法。
// 在每次请求中,调用 proxy.web(req, res config) 方法进行请求分发
var server = require('http').createServer(function (req, res) {
var url = req.url;
//console.log(url);
// 如果是后台Rest请求
//if ('/hrss-si-person/api' === url.slice(0, 19)||'/uaa' === url.slice(0, 4)) {
if ('/api' === url.slice(0, 4) ||'/uaa' === url.slice(0, 4)) {
// 代理到后台开发服务器
console.log('[api]',url);
proxy_end.web(req, res, {
target: 'http://localhost:8080'
});
return;
}else if ('/pile-dossier-app' === url.slice(0, 17)) {
// 代理到后台开发服务器
url = url.substr(17,url.length);
req.url = url;
console.log('[pile]',url);
proxy_end.web(req, res, {
target: 'http://185.25.105.82:8888'
});
return;
}
// 代理到前台开发服务器
console.log('[ui]',url);
proxy_front.web(req, res, {
target: 'http://localhost:9000'
});
});
console.log("server start up on port 80");
console.log("Sipub url= http://localhost:8000/#/login");
server.listen(80);