Angular-CLI代理不起作用 [英] Angular-CLI proxy doesn't work

查看:567
本文介绍了Angular-CLI代理不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的angular-cli我的项目",并创建了一个简单的虚拟服务.我希望此服务连接到本地计算机上的laravel后端.我发现到后端的Angular-CLI代理不起作用,甚至这些步骤对我不起作用. Chrome仍将转到localhost:4200.

I am using a fresh angular-cli "my-project" and have created a simple dummy service. I want this service connected to a laravel backend on my local machine. I found Angular-CLI proxy to backend doesn't work but even those steps are not working for me. Chrome is still going to localhost:4200.

我的服务

import { Injectable } from '@angular/core';
import {Http, Response} from '@angular/http';

@Injectable()
export class DummyService {

  constructor(private http: Http) {
    console.log('Hello dummyService');
  }

  getMessages() {
    return this.http.get('/backend/public/api/auth/login').map((res: Response) => res.json());
  }

}

我的proxy.config.json

my proxy.config.json

{
  "/backend": {
    "target": "http://localhost:81/laravelapi",
    "secure": false,
    "pathRewrite": {"^/backend" : ""},
    "changeOrigin": true,
    "logLevel": "debug"
  }
}

和我的package.json入门书

and my start propery of package.json

"start": "ng serve --proxy-config proxy.config.json",

启动时,我收到以下日志消息:

when starting up i get following log message:

** NG Live Development Server is running on http://localhost:4200 **
  0% compiling
 10% building modules 1/1 modules 0 active
 10% building modules 4/4 modules 0 active[HPM] Proxy created: /backend  ->  http://localhost:81/laravelapi
[HPM] Proxy rewrite rule created: "^/backend" ~> ""
[HPM] Subscribed to http-proxy events:  [ 'error', 'close' ]

 10% building modules 4/5 modules 1 active ...ct\node_modules\jquery\dist\jquery.js
 10% building modules 5/6 modules 1 active ...e_modules\metismenu\dist\metisMenu.js

,最后:

webpack: Compiled successfully.
[HPM] Rewriting path from "/backend/public/api/auth/login" to "/public/api/auth/login"
[HPM] GET /backend/public/api/auth/login ~> http://localhost:81/laravelapi

但是在浏览器中我得到了 GET http://localhost:4200/backend/public/api/auth/login 404(未找到)

but in browser i get GET http://localhost:4200/backend/public/api/auth/login 404 (Not Found)

所以它似乎不起作用. 我正在使用"@ angular/cli":"^ 1.0.0".

so it seems not to work. I am working with "@angular/cli": "^1.0.0".

有什么主意我做错了吗?

Any ideas what i do wrong?

我只想编写我的代码 /backend/public/api/auth/login 这些电话应该转到 http://localhost:81/laravelapi/public/api/auth/login 在我的本地机器上进行开发.

I just want to write inside my code /backend/public/api/auth/login and those calls should go to http://localhost:81/laravelapi/public/api/auth/login on my local machine for development.

任何建议! 彼得

推荐答案

您的URL是/backend/public/api/auth/login,因此您的代理应该是 即后端/*

Your URL's are /backend/public/api/auth/login, so your proxy should be this i.e backend/*

{
  "/backend/*": {
    "target": "http://localhost:81/laravelapi",
    "secure": false,
    "pathRewrite": {"^/backend" : ""},
    "changeOrigin": true,
    "logLevel": "debug"
  }
}

这篇关于Angular-CLI代理不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆