Angular 2.0服务调用不通过http-proxy-middleware

时间:2017-02-10 02:52:28

标签: angularjs proxy http-proxy-middleware

我有一个Angular 2.0应用程序,最近我使用以下bs-config.js添加了http-proxy-middleware:

set markup csv on
spool <filename>

select blah... from table

spool off

然后我有这样的服务:

var proxyMiddleware = require('http-proxy-middleware');

module.exports = {
    server: {
        port: 3000,
        middleware: {
            1: proxyMiddleware('/WorkLocation', {
                target: 'http://localhost/Perform/Company/WorkLocation',
                changeOrigin: false,
                logLevel: 'debug'
            }),
            2: require('connect-history-api-fallback')({index: '/index.html', verbose: true})
        }
    }
};

当我通过export class WorkLocationsService extends BaseRestfulService<WorkLocationItemModel[]> { private workLocationsServiceUrl: string = '/WorkLocation/EditList/111014/73442'; constructor(protected _http: Http) { super(_http) } public getWorkLocations(): Observable<WorkLocationItemModel[]> { return this.callApiEndPoint(null, this.workLocationsServiceUrl, HttpVerb.Get); }; 启动我的应用程序时,它会启动bs-config.js代理设置并看到以下输出:

npm start

所以看起来HPM代理正在进行正确的翻译但是在下面的行中我从我的WorkLocationService调用获得了GET 404错误。如果看起来配置正确,为什么我的服务不能通过代理? 我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

原来我的目标是错误的。

我有:

target: 'http://localhost/Perform/Company/Worklocation

并且必须是:

target: 'http://localhost/Perform/Company'

由于Worklocation已经是我传递的上下文的一部分。

这是现在正在为我工​​作的代码:

var proxyMiddleware = require('http-proxy-middleware');

module.exports = {
    server: {
        port: 3000,
        middleware: {
            1: proxyMiddleware('/WorkLocation', {
                target: 'http://localhost/Perform/Company',
                changeOrigin: true,
                logLevel: 'debug'
            }),
            2: require('connect-history-api-fallback')({index: '/index.html', verbose: true})
        }
    }
};