No index.html after angular reboot

I have an angular.js application with basic setup, for example: My index.html displays static things like Top / Sidebar. Inside I have a <div ng-view></div> to show partial html files like my home.html

After entering my application index.html with it, the Controller MainCtrl loads the same way as my home.html with the corresponding HomeCtrl . So all that should be.

After I applied the fix (so the reboot will not β€œforget” the user, and I will not log out - I based it on the example http://www.sitepoint.com/implementing-authentication-angular-applications/ ) I tried to reload my page. But now only my home.html .

NOTE. I posted the results below

Which makes sense, here is part of my app.js :

 $routeProvider.when('/', { templateUrl: 'views/home.html', controller: 'HomeCtrl', resolve: { auth: function ($q, authenticationSvc) { var userInfo = authenticationSvc.getUserInfo(); if (userInfo) { return $q.when(userInfo); } else { return $q.reject({ authenticated: false }); } } } }) .when('/login', { templateUrl: 'views/login.html', controller: 'LoginCtrl', login: true }) 

Thus, it is obvious that at startup / application only shows home.html . But how can I achieve what I already have when I enter a page, usually after login?

My main problem seems to be at the moment for me that I'm not quite sure how and why index.html loads normally when I log in. Here is what I do in my LoginCtrl after the SignIn process:

 $location.path("/"); 

I searched the Internet for some detailed explanations on this topic, but unfortunately I have not found anything useful yet. Can someone explain to me why this works in the first place and how I can make it work to reload pages? Thanks!

edit I read here ( https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode ) that I need to change my express configuration. I tried:

 app.all('/*', function(req, res, next) { res.sendfile('public/index.html', { root: __dirname }); }); 

I am using $locationProvider.html5Mode(true);

But when I log in, my page refreshes again until it works.

edit2: ok, after reading lots on html5mode and how to use it with an expression, I'm pretty sure that my error is in the backend, and not in Frontend.

Here are the parts of my server.js (express config)

 app.use(require('less-middleware')(path.join(__dirname, 'public'))); app.use(express.static(__dirname + '/public')); app.set('view engine', 'html'); app.set('views', __dirname + '/public/views/'); require('./routes')(app); // contains my other routes app.get('/', function(req, res) { res.render('index'); }); app.get('/views/:name', function (req, res) { var name = req.params.name; res.render('views/' + name); }); app.get('*', function(req, res) { res.redirect('/'); }); 

my folder structure:

 /server.js /routes /public/app.js /public/index.html /public/views/home.html /public/controllers/xxxCtrl.js 

I read a lot of posts about stackoverflow and other sites that basically say that

 app.get('*', function(req, res) { res.redirect('/'); }); 

should be enough to make it work, but for me it doesn't work.


Edit3: A brief description of my current problem:

When I reload the page with only partial html, for example, for example. home.html loading. Incomplete page with index.html . Here is a brief description of my code:

in the app.js (angular) configuration, I configured:

 $locationProvider.html5Mode(true); // I tried my app without html5mode, but then everything breaks - but I need the html5mode anyway $routeProvider.when('/', { templateUrl: 'views/home.html', controller: 'HomeCtrl', resolve: { auth: function ($q, authenticationSvc) { var userInfo = authenticationSvc.getUserInfo(); if (userInfo) { return $q.when(userInfo); } else { return $q.reject({ authenticated: false });} } } }) 

my authenticationSvc :

 .factory("authenticationSvc", ["$http", "$q", "$route", "$window", "$location", function ($http, $q, $route, $window, $location) { // login, logout function init() { console.log("init"); if ($window.sessionStorage["userInfo"]) { userInfo = JSON.parse($window.sessionStorage["userInfo"]); } } init(); 

and on the server side, server.js :

 app.get('/:name', function (req, res) { var name = req.params.name; res.sendFile('views/' + name); // res.sendFile(__dirname + '/public/index.html'); // uncommented, since it didn't work }); require('./routes')(app); // this is my last route: app.get('*', function (req, res) { res.redirect('/#' + req.originalUrl); }); 

from my understanding of this "should be" enough configuration for expression and angular for collaboration.

+5
source share
3 answers

First of all, in the angular route pattern URL add / at the beginning, for example templateUrl: '/views/home.html' . Since in your express application you are looking for /:name , which contains / at the beginning.

Further

 // ------ // try adding this app.get('/*', function (req, res) { res.sendFile(__dirname + '/public/index.html'); // uncommented, since it didn't work }); // Now what this does is that everytime a request is made it will send the index file // The asterisk will allow anything after it to pass through // Once it reaches the front end, Angular will process whats after "/" // It then will make an asynchronous request to the server depending on what // path is present after "/" // That is when app.get('/:name'... is called //------ app.get('/:name', function (req, res) { // This route will be called by angular from the front end after the above route has been processed var name = req.params.name; // since your template url is /views/home.html in the angular route, req.params.name will be equal to 'views/home.html'. So you will not need to add yet another 'views/' before the variable name res.sendFile(name); // res.sendFile('views/' + name); // res.sendFile(__dirname + '/public/index.html'); // uncommented, since it didn't work }); require('./routes')(app); // this is my last route: app.get('*', function (req, res) { res.redirect('/#' + req.originalUrl); }); 
0
source

Be sure to put all your client-side code in the / public folder and make it available through express-static middleware on your server.js server

 app.use(express.static(__dirname+'/public'))); 

Then add after defining all the routes

 app.route('/*') // this is the last route .get(function(req, res) { res.sendfile(__dirname+ '/public/index.html'); }); 

on a side note, __dirname is local to the file you are in, and always points to the containing folder. therefore, be careful when using __dirname in files located in subfolders.

0
source

after a lot of work, I created a new project, tried the routing explained, for example, the links / answers above, and then copied my old code. Unfortunately, I never found out what the problem is. There must have been a small part that I did not have enough to mention in this thread.

The solution if you encounter such a problem: look at the links and try to create a working sample project. At least it worked for me.

0
source

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


All Articles