Express.js and AngularJS routing parameters

720 views
Skip to first unread message

Michael Pateras

unread,
Apr 3, 2013, 11:29:13 PM4/3/13
to ang...@googlegroups.com

I'm trying to set up some routes with parameters in an AngularJS app backed by a node.js server using express. Node is set up to route everything that isn't explicitely found to a catch all:

app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(__dirname + "/public"));
app.use(function(request, response)
{
    console.log("catch all");
    writeFile("/public/index.htm", request, response);
});

And Angular has routes defined with 0-2 parameters:

$routeProvider
.when("/",
{
    templateUrl: "home.htm"
})
.when("/otherpage",
{
    templateUrl: "otherpage.htm"
})  
.when("/otherpage/:Var1",
{
    templateUrl: "otherpage.htm"
})  
.when("/otherpage/:Var1/:Var2",
{
    templateUrl: "otherpage.htm"
})
.otherwise(
{
    templateUrl: "home.htm"
});

$locationProvider.html5Mode(true);

/otherpage works, but while /otherpage/123 hits the catch all (I can see the console message), it crashes in the browser. Nothing is rendered and then Chrome says that it has a problem (and IE renders nothing just sits there).

I can understand the parameter not being passed, but why isn't the route at least happening without it? What do I need to do to set up URL parameters with express.js and AngularJS?

You can download the project here, if you want to examine it.

Thanks in advance.

Jeremy

unread,
Apr 4, 2013, 2:18:25 AM4/4/13
to ang...@googlegroups.com
It has to do with the relative urls. Once you are on localhost:3100/otherpage/1234, the base url is localhost:3100/otherpage, so it tries to ask for a static file at localhost:3100/otherpage/otherpage.htm. Since you have relative url paths, the current url base is prepended to the request. If you have an absolute path, this doesn't occur.

In your route definitions, you should put in the templateUrls a preceding / like: templateUrl: '/otherpage.htm' 

This tells node to start from the root to find the file, instead of from a relative folder location that doesn't exist.

Michael Pateras

unread,
Apr 4, 2013, 8:46:10 AM4/4/13
to ang...@googlegroups.com
That works perfectly. Thank you!


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/9ut1Ckc6xpU/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Henry Tao

unread,
Jul 17, 2013, 7:46:40 AM7/17/13
to ang...@googlegroups.com
Hi Michael Pateras!

In your example, what's happen if you refresh the page at http://127.0.0.1:3000/otherpage/page1?

In my case, it didn't load content of page 1. It loaded the home page (127.0.0.1:3000) and the url change to http://127.0.0.1:3000/otherpage?

I can not figure out what's the problem? 

Very appreciate any help!

Note: I used absolute path in templateUrl (http://127.0.0.1:3000/path/otherpage.html for example).

Michael Pateras

unread,
Jul 17, 2013, 9:08:20 AM7/17/13
to ang...@googlegroups.com
Sorry, I don't have access to that project anymore. Good luck!


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.

To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.

Adam Ulicki

unread,
Dec 8, 2013, 8:26:52 AM12/8/13
to ang...@googlegroups.com
Hi, I'm having this same problem right now. Have you maybe figured out how to handle it? 
Reply all
Reply to author
Forward
0 new messages