Express.js和AngularJS路由参数

时间:2013-04-04 02:38:45

标签: routing angularjs express

我正在尝试在使用express的node.js服务器支持的AngularJS应用程序中设置一些带参数的路由。设置节点以将未明确找到的所有内容路由到全部捕获:

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);
});

Angular的路由定义为0-2参数:

$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工作,但是当/ otherpage / 123点击catch all(我可以看到控制台消息)时,它会在浏览器中崩溃。什么都没有呈现,然后Chrome说它有一个问题(而IE只会让它没有任何东西)。

我可以理解参数没有被传递,但为什么不是没有它的路线?使用express.js和AngularJS设置URL参数需要做什么?

如果要检查项目,可以下载项目here

提前致谢。

1 个答案:

答案 0 :(得分:2)

问题是我没有在我的templateUrls前面加上一个foreslash。将它们改为:

.when("/otherpage/:Var1/:Var2",
{
    templateUrl: "/otherpage.htm"
})

工作得很好。