0

When I have the code like this:

phonecatApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/phones', {templateUrl: 'partials/phone-list.html',controller: 'PhoneListCtrl'
      }).
      when('/phones/:phoneId', {templateUrl: 'partials/phone-detail.html',controller: 'PhoneDetailCtrl'
      }).
      otherwise({redirectTo: '/phones'
      });
  }]);

I'm able to get the phoneslist by going to url: http://localhost:8000/app/#/phones

However, for removing # from url. if I replace the above code by this:

phonecatApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.
      when('/phones', {templateUrl: 'partials/phone-list.html',   controller: 'PhoneListCtrl'}).
      when('/phones/:phoneId', {templateUrl: 'partials/phone-detail.html', controller: 'PhoneDetailCtrl'}).
      otherwise({redirectTo: '/phones'});

    $locationProvider.html5Mode({
         enabled: true,
         requireBase: false
    });
  }]);

And I go to url:http://localhost:8000/app/phones/ I see the Index of /app/phones/instead of my normal webpage. Any idea where I'm going wrong ?

5
  • 1
    need to set server up to reroute to app entry point since you now have virtual directories in url Commented Apr 27, 2015 at 1:45
  • Thanks charlietfl, but can you please explain me how to do that ? Commented Apr 27, 2015 at 1:47
  • depends on server type, use htaccess for example in apache or web.config in IIS Commented Apr 27, 2015 at 1:48
  • I'm starting the server by npm start, following this tutorial: docs.angularjs.org/tutorial/step_07 Commented Apr 27, 2015 at 1:49
  • Thanks Claies, can you provide me the steps for getting HTML5Mode to work ? Commented Apr 27, 2015 at 2:08

2 Answers 2

2

If you look in the Packages.json for the angular-phonecat application, you will see "start": "http-server -a 0.0.0.0 -p 8000". This launches a basic node module http-server.

http-server is a simple, zero-configuration command-line http server.

It does not provide much in the way of options. However, you can use -P (note case) to create a proxy.

-P or --proxy Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com

so changing the start line to "start": "http-server -a 0.0.0.0 -p 8000 -P http://localhost:8000/app/" would work for testing at least.

When you make a request to http://localhost:8000/app/phones, the server will proxy it to your /app/ directory since it cannot be resolved. Once the app loads, the client side router will kick in, and use HTML5 rewrites to redirect you to the requested page.

In case of the angular-phonecat application, it was not designed with HTML5Mode in mind, and it contains a physical directory which conflicts with the virtual routes created in the example. No matter what server you deploy the app to, HTML5Mode expects that there is no physical directory match to your virtual routes, since those are resolved by the server before the Angular app can intervene.

Sign up to request clarification or add additional context in comments.

8 Comments

I made these changes: "start": "http-server -a 0.0.0.0 -p 8000 -P localhost:8000/app -i false", still localhost:8000/app/phones is showing the index page
I just looked again, this project has a physical directory mapped to /app/phones which would stop HTML5Mode from working correctly. You will either need to change your directory name, or change the virtual URL you are using.
Right, so I changed the directory phones to phones_json, and now I'm getting 'Not found' in the browser on going to localhost:8000/app/phones
did you restart the server after changing the start options? and you do need the full url, including the http:// for the Proxy option to work; I'm sure you included it but SO probably dropped it from the comment.
hmmm, try it without the auto-index option.
|
0

Okay. So I was solving this problem the wrong way. While following the Angular Tutorial they have used npm http-server and not the apache server. So, to enable html5 configuration, in this case this npm http-server server has to be configured - and I could not find any place where it's documented to do this.

So, I shifted from npm http-server to apache server and followed this: Server configurations for html5.

Again the bottom line is: Dont waste your time enabling html5 mode while using the basic module of http-server, which is given in official tutorial.

1 Comment

I'm glad you were able to get this working, and this was essentially what I was trying to say from the start, but I wanted to at least make an effort to try working it out. I did say in my answer that the basic server didn't have a lot of configuration options with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.