我已经成功地将我的角应用部署到Heroku上。我已经实现了proxy.conf.json文件来连接到后端API (它也部署在Heroku中及其Java中),并且它已经启动并运行。proxy.conf.json文件包含:
{
"/webapi/*": {
"target": "https://backend-api.herokuapp.com",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
} }还将package.json文件中的package.json命令更新为
"start": "ng serve --proxy-config proxy.config.json"这在本地使用localhost:4200非常好,但是当我将项目部署到heroku上时,后端调用不会被保存。供您参考,我将附加完整的package.json文件
{
"name": "myapp-client",
"version": "0.0.0",
"scripts": {
"heroku-postbuild": "ng build --aot --prod",
"ng": "ng",
"start": "npm run build & concurrently --kill-others \"npm run serve-api\" \"npm run serve\"",
"serve": "ng serve --proxy-config proxy.config.json",
"build": "ng build",
"serve-api": "node server.js",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"engines": {
"node": "14.8.0",
"npm": "6.14.7"
},
"private": true,
"dependencies": {
"@angular-devkit/build-angular": "^0.1000.6",
"@angular/animations": "~10.0.9",
"@angular/cli": "^10.0.6",
"@angular/common": "~10.0.9",
"@angular/compiler": "~10.0.9",
"@angular/compiler-cli": "^10.0.9",
"@angular/core": "~10.0.9",
"@angular/forms": "~10.0.9",
"@angular/platform-browser": "~10.0.9",
"@angular/platform-browser-dynamic": "~10.0.9",
"@angular/router": "~10.0.9",
"awesome-typescript-loader": "^5.2.1",
"bootstrap": "^4.5.2",
"concurrently": "^5.3.0",
"cors": "^2.8.5",
"d3": "^3.5.17",
"express": "^4.17.1",
"file-saver": "^2.0.2",
"font-awesome": "^4.7.0",
"jquery": "^3.5.1",
"ng2-nvd3": "^2.0.0",
"nvd3": "^1.8.6",
"popper.js": "^1.16.1",
"rxjs": "~6.6.2",
"source-map-loader": "^1.0.1",
"tslib": "^1.10.0",
"typescript": "~3.9.7",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.1000.6",
"@angular/router": "~10.0.9",
"@types/jasmine": "^3.5.12",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.12.54",
"codelyzer": "^5.1.2",
"concurrently": "^5.3.0",
"install": "^0.13.0",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~2.1.0",
"karma-jasmine": "~3.0.1",
"karma-jasmine-html-reporter": "^1.4.2",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"tslint": "^6.1.3"
},
"description": "This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.1.9.",
"main": "karma.conf.js",
"repository": {
"type": "git",
"url": "git+https://gitlab.com/myapp_tool/myapp-client.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gitlab.com/myapp_tool/myapp-client/issues"
},
"homepage": "https://gitlab.com/myapp_tool/myapp-client#readme"
}server.js文件包含:
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
var cors = require('cors');
const app = express();
var corsOptions = {
origin: 'https://backend-api.herokuapp.com',
optionsSuccessStatus: 200
}
//app.use(cors())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname + '/dist/myapp-client')));
//app.use('/webapi',api);
app.get('/webapi', cors(corsOptions), function (req, res, next) {
res.json({msg: 'This is CORS-enabled for Java API!'})
});
app.get('*', (req,res) => {
res.sendFile(path.join(__dirname+'/dist/myapp-client/index.html'));});
//app.get('*', function (req,res) {
//res.sendFile('index.html', {root: '/dist/myapp-client'}));});
//const port = process.env.PORT || '8080';
//app.set('port',port);
//const server = http.createServer(app);
app.listen(process.env.PORT || 8080, function (){
console.log('CORS-enabled API is running on Port:${process.env.PORT || 8080}')
});部署的角应用程序工作良好,直到它有静态页面。但是当它需要访问后端-api时,它是无法访问的。类似于当我想登录并发送用户I和密码时;请求应该如下所示:
https://myapp-client.herokuapp.com/login ==>> https://backend-api.herokuapp.com/webapi/login,但在浏览器控制台中,我发现它实际上是向:发送请求。
https://myapp-client.herokuapp.com/webapi/login角形应用程序无法读取后端-api地址;相反,它会将路径的其余部分添加为self(无法弄清楚为什么?)。
除了proxy.conf.json和cors之外,我找不到用于动态后端api调用的任何其他方法。抱歉,伙计们,太长了。但是我再也找不到有用的材料了,所以我把所有的事情都解释得更详细了。有人能帮忙吗?我还有最后期限。提前感谢您的帮助。
发布于 2020-08-20 13:47:09
尝试在代理配置中添加路径重写属性,如下所示-
{
context: '/webapi',
pathRewrite: { '^/webapi': '' },
target: "https://backend-api.herokuapp.com",
secure: false,
logLevel: "debug",
changeOrigin: true
}希望这能帮上忙!
https://stackoverflow.com/questions/63504254
复制相似问题