Angular-CLI proxy doesn't work

2019-04-12 03:11发布

问题:

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.

My service

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());
  }

}

my proxy.config.json

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

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

and at the end:

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

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

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

Any ideas what i do wrong?

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.

thx for any advice! Peter

回答1:

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"
  }
}


回答2:

Steps

1) In the Angular CLI application root folder create a new file called proxy.conf.json

Paste in the following JSON object.

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

2) Open the package.json file and change:

 "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e"
      },

... to:

"scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.conf.json",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },

3)

A) Open the Command Prompt

B) Go to the root folder of your Angular Cli application

C) type in npm start and press enter