AngularJS和ExpressJS路由问题?

时间:2013-07-15 17:57:33

标签: node.js angularjs express

我正在使用Angular,express,nodeJS进行Web应用程序开发。

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

当我尝试使用以下网址访问此应用程序时, http://www.domain:2000/view1

我收到了以下错误,

  

错误:无法查找视图“partials / undefined”       在Function.app.render(d:\ era \ startup \ learnnod \ restfull_angular2 \ node_modules \ express \ lib \ application.js:494:17)       在ServerResponse.res.render(d:\ era \ startup \ learnnod \ restfull_angular2 \ node_modules \ express \ lib \ response.js:756:7)       在exports.partials(d:\ era \ startup \ learnnod \ restfull_angular2 \ routes \ wines.js:104:9)       在回调(d:\ era \ startup \ learnnod \ restfull_angular2 \ node_modules \ express \ lib \ router \ index.js:161:37)       在param(d:\ era \ startup \ learnnod \ restfull_angular2 \ node_modules \ express \ lib \ router \ index.js:135:11)       在传递(d:\ era \ startup \ learnnod \ restfull_angular2 \ node_modules \ express \ lib \ router \ index.js:142:5)       在Router._dispatch(d:\ era \ startup \ learnnod \ restfull_angular2 \ node_modules \ express \ lib \ router \ index.js:170:5)       在Object.router(d:\ era \ startup \ learnnod \ restfull_angular2 \ node_modules \ express \ lib \ router \ index.js:33:10)       在下一个(d:\ era \ startup \ learnnod \ restfull_angular2 \ node_modules \ express \ node_modules \ connect \ lib \ proto.js:190:15)       在简历中(d:\ era \ startup \ learnnod \ restfull_angular2 \ node_modules \ express \ node_modules \ connect \ lib \ middleware \ static.js:60:7)

如果您需要更多信息,请告诉我

2 个答案:

答案 0 :(得分: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);
...

答案 1 :(得分: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');