6

I'm working on a Node app using Express as well as Angular. I'm using Angular for routing and have my routes setup like so:

app.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/', {
    templateUrl: '/partials/main'
    //controller: 'IndexController'
}).when('/discover', {
    templateUrl: '/partials/discover'
}).when('/user/home', {  //HERES THE PROBLEM CHILD!!!!!
    templateUrl: '/partials/user/home'
}).otherwise({
    redirectTo: '/'
});
}]).config(['$locationProvider', function ($locationProvider) {
    $locationProvider.html5Mode(true);
}]);

Now, whenever I try and call /user/home -- The page goes into an infinite loop and keeps reloading the controller. I can see in the node console that the page was called from partials/user/home which definitely contains a Jade file. I've checked other posts, most of them are solved with ass the / in the beginning of the partials path, that didn't help here. The page loads fine if I transfer home.jade into the /partials directory with no sub directory. Any ideas?

Update: It seems the infinite loop happens anytime I try to load a partial in any sub-directory of partials.

Per request:

Node -- App.js:

app.get('/', routes.index);

app.get('/partials/:name', routes.partials);
app.get('*', routes.index);

And routes/index.js

exports.index = function(req, res) {
res.render('index', { title: 'Open Innovation Station' });
}

exports.partials = function(req, res) {
res.render('partials/' + req.params.name);
}
1
  • Could you add your Node config? Commented Sep 1, 2013 at 0:14

2 Answers 2

12

Using html5Mode(true), you will have to concern yourself with any relative paths -- which you are using for your partials. The recursion in your case, I believe, could have been resolved by adding:

<base href="/"></base>

to your <head>.

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

2 Comments

sure did; thanks for this.. Silly mistake on my end for not having relative paths then wondering why things were including infinite.
I can't believe that html5Mode would alter a templateUrl that is specified with a slash in front of it. Would have expected that to be an absolute path. especially since the docs say so.
2

The routing rule that you have specified for partials will not match any requests to subdirectories within the partials folder e.g. /partials/folder/file. The routing path matcher treats / as a delimiter between variables. Therefore in order to support having sub folders within your partials directory you will have to add another rule to your app and define a function to handle rendering this template. This is shown below:

app.js

app.get('/partials/:directory/:file', routes.subpartials);

routes/index.js

exports.subpartials = function (req, res) {
  var file = req.params.file,
      directory = req.params.directory;
  res.render('partials/' + directory + '/' + file);
};

4 Comments

Thx for the response gordy. Im away from my machine for the night but will try in the AM.
It's best to use a catch all * in your partial route ('/partials/*'), then use req.params[0] to get the file that was requested. If the file directories are the same, you can just render the param.
Awesome solution. Just now getting into Angular routing, was recently using Express to handle everything. This sub-directory had me at a loss for a bit. I tried something like this earlier, app.get('/partials/user/:file' but I must've missed something. Thanks again!
To have a catch all partials work nicely with nested partials without any explicitly referencing nested directory, I will do this app.get('/partials/*', routes.subpartials); and render this way res.render('partials/' + req.params); in the subpartials function.

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.