我尝试了多种方式来正确构建 Heroku。
然后我决定这对我来说不是一个好主意。
现在,我在本地构建它,然后推送到 Heroku(或 git)。
为了在 Heroku 端使用 ENV var,我使用 PHP 来获取 env 并传递给应用索引。
在这个例子中,我得到了 api_url:
在 INDEX.HTML 中
<script>
let apiFromEnv = '<?php echo getenv("API_URL")?>'
if (apiFromEnv.indexOf('.com') > 0) {
document.apiURL = apiFromEnv
} else {
document.apiURL = null
}
</script>
在打字稿中使用时:
let apiURL = environment.API_URL;
if (document['apiURL']) {
apiURL = document['apiURL'];
}
在这行之后,我举个例子,把你的node应用切换到php
将您的应用构建包切换到 php,使用 composer.json
heroku 安装后,你必须将 index.html 名称更改为 index.php;这是 composer.json 文件(将其放在 Procfile 附近项目的根目录),它包含用于更改 Heroku 构建的 php。
{
"require": {
"php": "^7.1.3"
},
"scripts": {
"post-install-cmd": [
"mv dist/index.html dist/index.php"
]
}
}
我使用 .htaccess 来重定向服务器的规则和目录索引:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.php
RewriteRule ^ index.php
</IfModule>
最后一件事,不要忘记在 angular.json 资产复制规则中添加 .htaccess:
"architect": {
"build": {
.....,
"options": {
....
"assets": [
.....,
"src/.htaccess"
],
......