首页 文章

Angular proxy - proxy.conf.json中的意外标记

提问于
浏览
0

我有一个用于制作HttpRequests的Angular 6设置,但我需要通过代理运行它们 . 所以我在package.json旁边创建了一个proxy.conf.json:

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

并像我这样更新了我的package.json:

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

运行npm后,我得到:

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

版本:

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

任何人都知道我怎么能解决这个问题? JSON验证器说我的proxy.conf.json是一个有效的JSON .

EDIT
我在powershell中创建了proxy.conf.json文件,如下所示:

echo >> proxy.conf.json

这显然搞砸了文件 - 当我从Windows界面再次创建文件时,它工作 .

1 回答

  • 0

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

    {
      "\/api": {
        "target": "https://api.zonky.cz",
        "secure": false,
        "changeOrigin": true,
        "logLevel": "debug",
        "pathRewrite": {
          "^\/api": "/"
        }
      }
    }
    

相关问题