How can I deploy an Angular 2 application on tomcat server (Windows Server 2012)

61,055

Solution 1

I've found the solution:

I had to insert the right path to the bundle files!

I changed for example the src attribut of the index file from : src="inline.bundle.js"

to : src="dist/inline.bundle.js"

Just added the DIST FOLDEEEEEER ! Haha Thank you run yards for your help !

Solution 2

If your are using angular-cli command to create angular 2 project.Then cli has given some commands to deploy your application into production. Use command like ng build --prod on your project directory and it will generate dist folder in your directory.enter image description here

Go to inside dist folder and change the base href like href="." in index.html. After that copy dist folder in your tomcat root directory and run the server.enter image description here. you can see below screen shot our application running on tomcat server. enter image description here

Solution 3

You can change one place in the index.html: <base href="/">
Make the base href to be your relative path form webapps folder in tomcat to the folder that includes the index.html of your angular app.

In your case, change it to: <base href="./dist">
No other changes are needed.

Solution 4

Just build it out using any of the build tasks(dev will include some source maps, and don't worry about having node on the server as node is only needed for the development tooling. Then just copy over all the files in the dist folder. Have a look in the index.html and make sure that the references to the various scrips are correct. The only thing the app needs to run is the scrips in the index, no node is needed.

Solution 5

  1. Run this command ng build --prod on your terminal
  2. After check one folder is created in parallel of src dist and go to dist folder and go to inside of your project folder and select all files and paste in inside of WebContant and parallel in web-INF and go to inside on index.html page and replace / from . in like href="." and go to url example: http://localhost:8080/ProjectName/#/URL and check it.
Share:
61,055
altd
Author by

altd

Updated on June 13, 2020

Comments

  • altd
    altd almost 4 years

    I'm new in AngularJS 2 framework and I'm stuck!

    I want to deploy an application (quickstart for example) on a Windows Server 2012. I have already installed Apache tomcat on it. What I did : I used the command npm build (with and without "-prod") in order to generate the dist folder (output : index.html & the bundle files) then I putted it into 'webapps' folder in tomcat, then I tried to reach the page using my browser, the index page is showing me "Loading..."! I think that angular is not working...

    I tried to install NodeJs on the server then generate a simple project (npm install -g angular-cli) and run it on the server.. it works on the localhost (server) but on my machine using "IP_of_sv:Port" Chrome gives me an error "ERR_CONNECTION_REFUSED"!

    Can you help me please with simple explanations please?

    Thank you in advance!

  • altd
    altd over 7 years
    Thank you for your answer. I still have the problem ! I used ng build to generate the dist folder, [I checked if all the scripts are in the correct format, just like you told me! I've found no difference and I think it's ok!][1] But on my browser I got the 404 error message : [It cannot find the bundle files!][2] I've used some combinations.. (creating a new folder "test/x.js" ,, changing the path into dist/test/x.js but no result !) Thank you in advance for your help! [1]: i.stack.imgur.com/DP3nF.png [2]: i.stack.imgur.com/haflx.png
  • Hardik Patel
    Hardik Patel almost 7 years
    It works seamlessly. But is there any way so that i would not need to change href value in index.html
  • Deepak Goel
    Deepak Goel over 6 years
    pass it like ng build --base-href=/BaseRef/
  • Abdullah Khan
    Abdullah Khan about 6 years
    Any explanations as to why changing the base href works. I want to know why it started working?
  • Philip Rego
    Philip Rego about 6 years
    I needed <base href="./">
  • Mahi
    Mahi almost 6 years
    @Balaji, Thanks, but , as you can see on deploying dist folder (build folder) on tomcat server, app runs on http. How to run our app on https ?
  • Mahi
    Mahi almost 6 years
    @Ahmad, Thanks, but , as you can see on deploying dist folder (build folder) on tomcat server, app runs on http. How to run our app on https ?
  • Mahi
    Mahi almost 6 years
    Thanks, but , as you can see on deploying dist folder (build folder) on tomcat server, app runs on http. How to run our app on https ?
  • Balaji B
    Balaji B almost 6 years
    @Ahmadmnzr, Please can you check this stackoverflow.com/questions/39210467/…
  • Mahi
    Mahi almost 6 years
    Thanks, it is for serving purpose and on server, we need to configure our tomcat.
  • Pramod
    Pramod almost 5 years
    I have attempted the same steps but I got error msg when page refresh "HTTP Status 404" can someone help me to resolve.