简体   繁体   中英

Angular JS: How to get rid of app/index.html in the url and stop displaying directory content

I forked the angular seed project to try angularJS, and I'm facing some problems with URLs. How can I get rid of the app/index.html part to access the index.html file, so that localhost:8000/app/index.html becomes localhost:8000 ? Also, it looks like the routes I defined are ignored.

/* app/js/app.js */

var myApp = angular.module('myApp', [
  'ngRoute',
  'myAppControllers'
]);

myApp.config(['$routeProvider', '$locationProvider',
                function($routeProvider, $locationProvider) {
  /* I can't even find how to access these urls */
  $routeProvider.when('/', {
    templateUrl: 'partials/home.html',
    controller: 'HomeCtrl'
  })
  .when('/navigation', {
    templateUrl: 'partials/file-navigation.html',
    controller: 'FileNavigationCtrl'
  })
  .otherwise({
    redirectTo: '/'
  });

  $locationProvider.html5Mode(true);
}]);

/* app/js/controllers.js */

var myAppControllers = angular.module('myAppControllers', []);

myAppControllers.controller('FileNavigationCtrl', function($scope) {
  $scope.files = [ /* some files */ ];
});

myAppControllers.controller('HomeCtrl', function($scope) {
  $scope.user = 'TestUser';
});

How can I get to the navigation page for example ? I tried:

  • localhost:8000/navigation
  • localhost:8000/app/navigation
  • localhost:8000/app/index.html/navigation
  • localhost:8000/app/index.html#/navigation

and none of them work, the last one however, change the URL to localhost:8000/#%2Fnavigation, while the others return a 404 error.

To get rid of index.html you need to change the server part of this example, https://github.com/angular/angular-seed/blob/master/scripts/web-server.js

The server attempts to parse URI and serve either file from disk, or a directory listing. You may add logic to serve index.html if / is requested, something along the lines

if (path === './') { process.chdir('app') return self.sendFile_(req, res, 'index.html') }

However, I don't think you should use it in real life. Get yourself express and start from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM