altd
altd

Reputation: 181

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

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!

Upvotes: 14

Views: 66390

Answers (5)

Bheem  Singh
Bheem Singh

Reputation: 707

  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.

Upvotes: 1

Balaji B
Balaji B

Reputation: 339

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

Upvotes: 29

Ahmad Agbaryah
Ahmad Agbaryah

Reputation: 532

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.

Upvotes: 6

altd
altd

Reputation: 181

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 !

Upvotes: 3

rtn
rtn

Reputation: 2034

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.

Upvotes: 3

Related Questions