How can I deploy an Angular 2 application on tomca

2019-02-06 15:38发布

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!

6条回答
Ridiculous、
2楼-- · 2019-02-06 16:15

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.

查看更多
乱世女痞
3楼-- · 2019-02-06 16:19

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

查看更多
走好不送
4楼-- · 2019-02-06 16:29
  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.
查看更多
做自己的国王
5楼-- · 2019-02-06 16:33

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.

查看更多
乱世女痞
6楼-- · 2019-02-06 16:35

Use the link where I posted the solution for that. Please check and let me know if you have any other issues. Deploy Angular 2 App with Webpack to Tomcat - 404 Errors

查看更多
走好不送
7楼-- · 2019-02-06 16:36

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 !

查看更多
登录 后发表回答