Angularjs index.html in views folder

I have little confusion. Using Nodejs

folder image structure attached.

If I put index.html in the root of the client folder, than everything works fine.

On the other hand, if I move index.html in the views folder, as in the image, than js files are not loading, and index.html is loading.

Nodejs - server.js

 app.configure(function () { app.set('port', process.env.PORT || 3000); app.use(express.favicon()); app.use(express.cookieParser()); app.use(express.bodyParser()); app.use(express.logger('dev')); //tiny, short, default app.use(express.methodOverride()); app.use(express.cookieSession({secret: "sdfr"})); //app.set('views', __dirname + '/client/views/'); app.use(express.static(__dirname + '/client/views')); }); 

app.js

 app.config(['$routeProvider', function ($routeProvider) { $routeProvider.when('/', { templateUrl: 'partials/home.html', controller: 'HomeCtrl' }); $routeProvider.when('/login', { templateUrl: 'partials/login.html', controller: 'LoginCtrl' }); $routeProvider.when('/register', { templateUrl: 'partials/register.html', controller: 'RegisterCtrl' }); $routeProvider.when('/404', { templateUrl: 'partials/404.html' }); $routeProvider.otherwise({redirectTo: '/404'}); //$locationProvider.html5Mode(true); }]) 

index.html

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html ng-app="contactManager"> <head> <meta charset="utf-8"> <title>Angular Demo</title> </head> <body> <a href="#/">Home</a><br/> <a href="#/login">Login</a><br/> <a href="#/register">Register</a><br/> <a href="#/private">Private</a><br/> <a href="#/admin">Admin</a><br/> <a href="#/404">404</a><br/> <div> <div ng-view></div> </div> <script src="../lib/vendor/angularjs/1.1.5/angular.min.js"></script> <script src="../js/app.js"></script> <script src="../js/controllers.js"></script> </body> 

Folder structure

Error

+3
source share
1 answer

You currently DO NOT include JS in express.static . The included JS sources in ../js from your index.html file go nowhere, because client/views is the only directory served by express.static .

You just need to include the entire client in your static one, otherwise you will need to include each directory in your static provider.

app.use(express.static(__dirname + '/client/views')); means that you are serving any of / client / views, but nothing outside this directory.

app.use(express.static(__dirname + '/client/js')); will allow you to maintain the JS folder, but root will access it. You can use TWO static providers, but the first will win in case of conflict. You can also do app.use(express.static('/js', __dirname + '/client/js')); so that all of you can access the client / js at yoursite.com/js , but that seems odd to me.

Read about using express.static here: http://expressjs.com/api.html#middleware

+3
source

Source: https://habr.com/ru/post/1206831/


All Articles