Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy angular-cli app on iis

I have simple angular2-cli app (one page with model driven form - no router involved). With "ng serve" all works fine. I made production version with ng build --product. I copied all ./dist folder content into new folder under C:\inetpub\wwwroot. I made virtual app from IIS managment console. Defualt app file is index.html. I browse to app uri and i get only page with "Loading...". I try build without --product switch (only ng build) but result is the same. Angular app is not loading. Is there anything else needed to publish angular app on IIS?

like image 856
PaulStanek Avatar asked Dec 23 '16 16:12

PaulStanek


People also ask

Can we deploy Angular app in Tomcat?

To deploy an angular application in tomcat we need to build the application using the ng tool. For this demo, I am going to build a simple default angular application using below command. To create an angular application run the following command in command prompt as shown below.


2 Answers

Here is how I solve this situation;

  • Create project with angular-cli
  • Build your application with ng build
  • Open IIS, create new virtual directory and show dist folder
  • Set base href in your index.html from / to /yourAliasNameOnIIS
  • Use this web.config for redirecting requests to your index.html page

    <system.webServer> <rewrite>   <rules>     <rule name="AngularJS Routes" stopProcessing="true">       <match url=".*" />       <conditions logicalGrouping="MatchAll">         <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />         <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />         <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />       </conditions>       <action type="Rewrite" url="/yourAliasNameOnIIS" />     </rule>   </rules> </rewrite> 

  • Convert your virtual directory to a web application

You can also use ng build --deploy-url "/yourAliasNameOnIIS" to change src path in dist/index.html.

I hope it helps!

like image 122
ulubeyn Avatar answered Sep 24 '22 13:09

ulubeyn


When you open the dev tools of your browser, you would have seen 404 messages when the app was trying to donwload the js, css etc files

You need to set the base href in index.html to

<base href="./">

this will make sure the base ref is relative to where your website lives in IIS. You also need to use hash location strategy otherwise, IIS will intercept your ng2 router URL changes and try to find a controller/action for the URL.

under the imports of your app.module.ts:

RouterModule.forRoot(routerConfig, { useHash: true }) 

I have done these 2 steps and all is working perfectly on Azure VM with IIS. Doing it this way also means it you do not have to put your SPA on the root and you can have multiple SPA's running happily next to each other (in different websites on IIS)

like image 22
Jan Feyen Avatar answered Sep 21 '22 13:09

Jan Feyen