1

I have a working Angular 6 setup for making HttpRequests, but I need to run them through the proxy. So I created a proxy.conf.json next to my package.json:

{
  "/loans/": {
    "target" : "https://api.zonky.cz",
    "secure" : false,
    "changeOrigin" : true
  }
}

And updated my package.json like so:

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

After running npm start I get:

C:\Users\Tom\Javascript\testhttp\proxy.conf.json: Unexpected token � in JSON at position 0
SyntaxError: C:\Users\Tom\Javascript\testhttp\proxy.conf.json: Unexpected token � in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.Module._extensions..json (module.js:671:27)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at DevServerBuilder._addProxyConfig (C:\Users\Tom\Javascript\testhttp\node_modules\@angular-devkit\build-angular\src\dev-server\index.js:244:27)
    at DevServerBuilder._buildServerConfig (C:\Users\Tom\Javascript\testhttp\node_modules\@angular-devkit\build-angular\src\dev-server\index.js:177:18)
    at Observable.rxjs_1.Observable.obs [as _subscribe] (C:\Users\Tom\Javascript\testhttp\node_modules\@angular-devkit\build-angular\src\dev-server\index.js:42:47)
    at Observable._trySubscribe (C:\Users\Tom\Javascript\testhttp\node_modules\rxjs\internal\Observable.js:43:25)
    at Observable.subscribe (C:\Users\Tom\Javascript\testhttp\node_modules\rxjs\internal\Observable.js:29:22)
    at C:\Users\Tom\Javascript\testhttp\node_modules\rxjs\internal\util\subscribeTo.js:22:31
    at Object.subscribeToResult (C:\Users\Tom\Javascript\testhttp\node_modules\rxjs\internal\util\subscribeToResult.js:7:45)
    at MergeMapSubscriber._innerSub (C:\Users\Tom\Javascript\testhttp\node_modules\rxjs\internal\operators\mergeMap.js:75:38)
    at MergeMapSubscriber._tryNext (C:\Users\Tom\Javascript\testhttp\node_modules\rxjs\internal\operators\mergeMap.js:72:14)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! testhttp@0.0.0 start: `ng serve --proxy-config proxy.conf.json`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the testhttp@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Tom\AppData\Roaming\npm-cache\_logs\2018-06-17T13_52_55_968Z-debug.log

Versions:

Angular CLI: 6.0.8
Node: 8.11.2
OS: win32 x64
Angular: 6.0.5
Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.6.8
@angular-devkit/build-angular     0.6.8
@angular-devkit/build-optimizer   0.6.8
@angular-devkit/core              0.6.8
@angular-devkit/schematics        0.6.8
@angular/cli                      6.0.8
@ngtools/webpack                  6.0.8
@schematics/angular               0.6.8
@schematics/update                0.6.8
rxjs                              6.2.1
typescript                        2.7.2
webpack                           4.8.3

Anybody please has any idea how could I fix this? JSON validators say my proxy.conf.json is a valid JSON.

EDIT
I have created the proxy.conf.json file in powershell like so:

echo >> proxy.conf.json

which apparently messed up the file - when I created the file again from windows interface it works.

4

1 回答 1

1

尝试创建新文件并复制到新文件:

{
  "\/api": {
    "target": "https://api.zonky.cz",
    "secure": false,
    "changeOrigin": true,
    "logLevel": "debug",
    "pathRewrite": {
      "^\/api": "/"
    }
  }
}
于 2018-06-17T14:58:38.443 回答