2

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

角码:

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

项目文件夹结构: 文件夹结构

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

我收到以下错误,

错误:无法在 ServerResponse.res.render (d:\era\startup\learnnod\restfull_angular2\node_modules\express\lib\application.js:494:17) 的 Function.app.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) 在参数 (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)

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

4

2 回答 2

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);
...
于 2013-07-15T18:04:17.013 回答
0

在您的 wine.js 中,您使用的是 parameter 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');

于 2013-07-15T18:20:57.700 回答