Angular 2 configuration issues with Azure Slot deployments

自闭症网瘾萝莉.ら 提交于 2019-12-02 09:55:31

To achieve your requirement, you can just put the following PHP file to your site root folder. Then send a GET request to endpoint http://<websitename>.azurewebsites.net/appSettings.php via your Angular app. This will give you a JSON Object that contains all App settings.

appSettings.php

<?php

$appSettings = [];

foreach ($_SERVER as $key => $value) {

    if(preg_match('/^APPSETTING_/', $key)) {
        $appSettings[str_replace('APPSETTING_', '', $key)] = $value;
    }
}

header('Content-Type: application/json');
echo json_encode($appSettings);

Maybe somebody still need it. I propose to stay closer to chosen technology and expose node.js api similar to previous php answer.

I create endpoint http://<websitename>.azurewebsites.net/app-settings exposed in Azure AppService as following:

Add index.js file into src directory in your Angular code:

var http = require('http');
var server = http.createServer(function (request, response) {
    response.writeHead(200, { "Content-Type": "application/json" });
    var result = {};
    result.someAppSetting = process.env.SomeApplicationSetting;
    var jsonResult = JSON.stringify(result);
    response.end(jsonResult);
});
var port = process.env.PORT || 1337;
server.listen(port);
console.log("Server exposing api for reading application settings running at http://localhost:%d", port);

So, response contains json with SomeApplicationSetting app setting retrieved from env vars by process.env.SomeApplicationSetting. Of course you can have other strategy of exposing variables like adding only some prefixed settings to json or any other.

Add index.js to assets in angular.json:

"assets": [
  "src/favicon.ico",
  "src/assets",
  "src/web.config",
  "src/index.js"
],

Then in web.config add following rewrite rule:

<rule name="Read app settings" stopProcessing="true">
  <match url="app-settings" />
  <action type="Rewrite" url="index.js"/>
</rule>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!