Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy ng2 app after ng-build

I've never understood what comes after the ng build command. I mean, how can you actually load your built app on a normal server and access it? Until now I've found only topics talking about the ng serve and ng build, saying that after the ng build you're done. But that's not true.

If I try to build and then navigate to the folder, I get a lot of errors in the console about files not found etc... not to mention errors about unmatched routes and so on.

The fact is that the app before the build using ng serve works like a charm, but I can't go to the client and say: "Well, you have to use ng serve".

Jokes apart, how do I actually serve my built app without using GitHub pages and other hosts, but using my own server?

I swear that I've searched a lot on this topic, but I've never found a "stable" solution for it.

Thanks for any possible tip. I'm freaking out with this thing.

like image 422
Caius Avatar asked Oct 15 '25 02:10

Caius


2 Answers

Because of new <base href="/"> attribute in the html, app is looking for your assets in the respective root folder.

So try to remove and run so that it will work and load the base href in run time.

You can use the below code to get it working

import {APP_BASE_HREF} from '@angular/common';
{provide: APP_BASE_HREF, useValue : '/' }

or using javascript

document.write('<base href="' + document.location + '" />');
like image 78
ankesh jain Avatar answered Oct 17 '25 15:10

ankesh jain


If you use Angular CLI: ng build Navigate to your app folder /dist

enter image description here

If you want to run it on your PC with no hassle just download USB Webserver and just copy the files to the root folder and run the server, you can port it to your client via usb or on client PC.

To run the app on a server: copy everything from dist folder + all your assets ( bootstrap.css or image folder or fonts folder)

enter image description here

http://162.244.83.64/

===========================================

If you are trying to run the app from local PC keep in mind that the app is looking for your assets in the root folder (C:/, D:/).

Open your Index.html file and edit the to point to the correct path, you don't need to do this on a server or hosting.

like image 20
Avram Virgil Avatar answered Oct 17 '25 17:10

Avram Virgil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!