twaldron
twaldron

Reputation: 2752

NG Serve with a VirtualPath or subdirectory

We run our angular application in IIS as a virtual application "myapplication"

In our angular app I have set in index.html:

<base href="/myapplication/"> 

It is working in IIS fine. However now NG serve does not work. As it can't find all the bundles under http://localhost/myapplication/ because the CLI isn't running under that path.

I have tried

ng serve --base-href /myapplication/

and browse to http://localhost:4200/myapplication/

I still see the same errors.

How do I tell the CLI to serve the application under "http://localhost:4200/myapplication/"?

Upvotes: 4

Views: 5177

Answers (2)

Danziger
Danziger

Reputation: 21161

If you want to differentiate the base paths used for routing and assets, you can use both --base-href and --deploy-url:

ng serve --base-href /myapplication --deploy-url /static

Your application should now be accessible at http://localhost:4200/myapplication while your static files will be served from http://localhost:4200/static.

You can also add these as baseHref and deployUrl in your workspace.json config file.

Most of the time baseHref should be enough though.

Upvotes: 2

DeborahK
DeborahK

Reputation: 60518

Change the base href to just this:

<base href="/">

That sets the default for development time.

Then when you build for production, use:

ng serve --base-href /myapplication/

That will set it correctly when it builds the files for deployment.

Upvotes: 5

Related Questions