Home > Blockchain >  Angular: serving application on custom context-root with npm start on localhost
Angular: serving application on custom context-root with npm start on localhost

Time:10-26

I have an Angular application which starts on port 4300 with this configuration:

"scripts": {
  "ng": "ng",
  "start": "ng serve --port 4300 --host 0.0.0.0 --proxy-config proxy.conf.json",
  "build": "ng build",
  "test": "ng test",
  "lint": "ng lint",
  "e2e": "ng e2e"
}

The app is reachable to the url:

http://localhost:4300/#/index.html

I want to serve it on a context-root, like: "/myApp/"

The application has to start always with npm start, and I want to reach the url:

http://localhost:4300/myApp/#/index.html

How can I do it?

Everything I tried doesn't work:

  • setting <base href="/myApp/"> doesn't work
  • setting "build": "ng build --prod --base-href=myApp" doesn't work

Any help will be appreciated.

CodePudding user response:

Try ng build --prod --base-href /myApp/, or same but with ng serve

I think the forward slashes are important here

CodePudding user response:

Add baseHref, deployUrl in your angular.json file. Adding code snippet:

"serve": {
     "options": {
        "baseHref": "/myApp/",
        "deployUrl": "/myApp/"
      }
    }

This <base href="/myApp/"> will remain as is.

You can remove --base-href=myApp from the build command

  • Related