У меня проблема с прокси на внешний API после создания приложения и загрузки его на сервер.
Начните с самого начала:
- В моем
proxy.config.json
I укажите следующую конфигурацию:
{
"/api/*": {
"target": "http://api.example.com/api/",
"secure": false,
"logLevel": "debug",
"pathRewrite": {
"^/api": ""
},
"changeOrigin": true
}
}
Я вызываю этот API, используя функцию ниже в my.service.ts:
loginUser(userName:string,Password:string){
let loginInfo = {Email:userName,Password:Password}
return this.http.post('/api/authenticate',loginInfo,{headers:{'Content-Type': 'application/json-patch+json'},responseType:'text'})
.pipe(map(data => {
this.currentUser = data
this.isLoggedIn = true
return true
}))
.pipe(catchError(err => {
this.errorResponse.push({status:err.status,statusText:err.statusText,message:err.message})
return this.errorResponse
}))
}
Когда я звоню по этому номеру с localhost
, это работает как заклинание. В консоли браузера я получаю: http://localhost:4200/api/authenticate
Но когда я создаю приложение и работаю с ним на своем хостинг-сервере, я получаю сообщение об ошибке POST http://example.com/api/authenticate 404 (Not Found)
, и оно публикует этот URL -> http://example.com/api/authenticate
Заголовок
Некоторые сведения о приложении:
- Приложение внешнего интерфейса обслуживается
www.example.com
- Все API находятся на
api.example.com
- На локальном хосте он работает на хост-сервере, он не
Пожалуйста, дайте мне знать, если вам нужна дополнительная информация, я отредактирую этот пост, чтобы добавить эту информацию.
ОБНОВЛЕНИЕ:
angular.json
файл:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"my-app-name": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/my-app-name",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": false,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/pink-bluegrey.css",
"src/styles.css",
"src/styles/bootstrap-4.4.1-dist/css/bootstrap.min.css"
],
"scripts": [
"./node_modules/jquery/dist/jquery.min.js",
"./node_modules/bootstrap/dist/js/bootstrap.min.js"<br>
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "6kb",
"maximumError": "10kb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "my-app-name:build",
"proxyConfig": "proxy.config.json"
},
"configurations": {
"production": {
"browserTarget": "my-app-name:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "my-app-name:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/pink-bluegrey.css",
"src/styles.css"
],
"scripts": []
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
}
},
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "my-app-name:serve"
},
"configurations": {
"production": {
"devServerTarget": "my-app-name:serve:production"
}
}
}
}
}
},
"defaultProject": "my-app-name"
}