2013-07-15 108 views
2

我正在使用Angular,express,nodeJS进行Web应用程序开发。AngularJS和ExpressJS路由问题?

Anuglar代码:

'use strict'; 
// 
Declare app level module which depends on filters, and services 
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'myApp.controllers']). 
    config(['$routeProvider', function($routeProvider) { 
    $routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'}); 
    $routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'}); 
    $routeProvider.otherwise({redirectTo: '/view1'}); 
    }]).config(['$locationProvider', function($locationProvider) { 
    $locationProvider.html5Mode(true); 
}]);; 


angular.module('modelDemo', []). 
config(function ($routeProvider) { 
    $routeProvider. 
    when('/', { 
     controller: 'AuthorListCtrl', 
     templateUrl: 'partials/list.html' 
    }); 
    $routeProvider. 
    when('/view1', { 
     controller: 'MyCtrl1', 
     templateUrl: 'partials/partial1.html' 
    }); 
    $routeProvider.otherwise({redirectTo: '/view1'}) 
}); 

var app = angular.module('app', ['myApp', 'modelDemo']); 

快速代码

// all environments 
app.set('port', process.env.PORT || 2000); 
app.set('views', __dirname + '/views'); 
app.set('view engine', 'jade'); 
app.use(express.favicon()); 
app.use(express.logger('dev')); 
app.use(express.bodyParser()); 
app.use(express.methodOverride()); 
app.use(express.static(path.join(__dirname, 'public'))); 
app.use(app.router); 

app.get('/', wine.index); 
app.get('/partials/:name', wine.partials); 
app.get('/wines', wine.findAll); 
///app.get('/view1', wine.index); 
app.get('/wines/:id', wine.findById); 
app.post('/wines', wine.addWine); 
app.put('/wines/:id', wine.updateWine); 
app.delete('/wines/:id', wine.deleteWine); 
app.get('/view1', wine.partials); 
//app.get('/view1/:name', wine.partials); 

2)

exports.index = function(req, res){ 
    console.log("request url---"+req.url); 
    res.render('partials/' + req.params.name); 
}; 

exports.partials = function(req, res) { 
    console.log("request url---"+req.url); 
    console.log(req.params.name); 
    res.render('partials/' + req.params.name); 
}; 

项目文件夹结构: Folder stru

当我尝试用下面的URL访问这个应用程序, http://www.domain:2000/view1

我收到以下错误,

Error: Failed to lookup view "partials/undefined" at Function.app.render (d:\era\startup\learnnod\restfull_angular2\node_modules\express\lib\application.js:494:17) at ServerResponse.res.render (d:\era\startup\learnnod\restfull_angular2\node_modules\express\lib\response.js:756:7) at exports.partials (d:\era\startup\learnnod\restfull_angular2\routes\wines.js:104:9) at callbacks (d:\era\startup\learnnod\restfull_angular2\node_modules\express\lib\router\index.js:161:37) at param (d:\era\startup\learnnod\restfull_angular2\node_modules\express\lib\router\index.js:135:11) at pass (d:\era\startup\learnnod\restfull_angular2\node_modules\express\lib\router\index.js:142:5) at Router._dispatch (d:\era\startup\learnnod\restfull_angular2\node_modules\express\lib\router\index.js:170:5) at Object.router (d:\era\startup\learnnod\restfull_angular2\node_modules\express\lib\router\index.js:33:10) at next (d:\era\startup\learnnod\restfull_angular2\node_modules\express\node_modules\connect\lib\proto.js:190:15) at resume (d:\era\startup\learnnod\restfull_angular2\node_modules\express\node_modules\connect\lib\middleware\static.js:60:7)

请让我知道如果你需要它的详细信息

+0

什么是代码指向这里(d:\时代\启动\ learnnod \ restfull_angular2 \路径\ wines.js:104:9)在回调 – user568109

+0

新增wines.js: 104代码在Quesiton, – niran

+0

你可能想删除app.set('view engine','jade'); –

回答

5

问题是,您正在尝试处理您的角码和您的快递代码中的路线。为了让它按预期工作,您需要将所有非部分/非API路线导向到您的index.html文件。

试着做这样的事情:

wine.js(您的明确路线文件)

exports.index = function(req, res) { 
    res.sendfile(__dirname + "/public/index.html"); // updated to reflect dir structure 
}; 

app.js(您的明确程序文件)

... 
// make this your last route and remove your '/view1' route 
app.get('*', wine.index); 
... 
+0

不,我不想在这个项目中使用Jade – niran

+0

这是否意味着yourdirectory = public,根据我的问题 – niran

+0

中显示的文件夹结构我相信这是正确的。 –

0

在您的wine.js中,您使用参数name,但在您的路线中,您已评论一部分。

app.get('/view1', wine.partials); 
//app.get('/view1/:name', wine.partials); 

既然你没有指定帕拉姆:name的路线,当您尝试与req.params.name访问它给出了定义。并且您查找不存在的文件undefined

Error: Failed to lookup view "partials/undefined" at Function.app.render 

你可能想res.render('partials/view1');

+0

不,它不会工作。我也尝试过这个选项。仍然有同样的错误。我认为,anuglr路由器和快速路由器 – niran

+0

/view1 /之间存在不匹配:名称与“http://www.domain:2000/view1”不匹配。你是否在传递一些名称的变量来测试它?这是一个快速路由器错误。 – user568109

+0

是的,根据角路由器定义view1 = partials/partial1.html。这意味着,当你说/ view1时,它只不过是一个www.domain.com/partial/partials.html。 – niran